I am trying to show on the screen a date in the format such as Monday October 4, 2013 using the data from MySQL table. The date is stored in DATE format. I am using the following and I get an error:
echo "<td class='date' colspan='8'>" .date($row['draw_date'],'l F j, Y'). "</td>";
Please tell me what I am doing wrong.
You have the parameters backwards for date()
and you need to convert your date to a timestamp before using it in the date()
function.:
date($row['draw_date'],'l F j, Y')
should be
date('l F j, Y', strtotime($row['draw_date']))
The date()
function expects a Unix timestamp as its second parameter and you're now supplying the format as the second parameter.
string date ( string $format [, int $timestamp = time() ] )
You need to first convert your date string to a Unix timestamp using strtotime()
and you can use it in date()
to get the date in required format:
date('l F j, Y', strtotime($row['draw_date']))
Refer to the documentation for more information: date()
MySQL is going to be returning the date in string format, e.g. 2013-10-07
. You can NOT use that string as the timestamp in PHP's date()
function. That function expects a UNIX time stamp, which is a simple integer. Either format your date in MySQL using date_format() and simply output the string, or use MySQL's unix_timestamp() to convert the native DATE into a unix timestamp which you CAN feed into PHP's date system.
I used the following from different suggestions and it seems to work. Thanks for your help.
$row['display_date'] = strtotime($row['date']);
echo "<td class='date' colspan='8'>".date('D M d, Y', $row['display_date']). "</td>";