Reputation: 19
i have a date in my mysql database that i would like to read into a variable/array and then convert from mm/dd/yyyy hh:mm:ss to DD MMM YYYY. Is this possible and if so, how would i go about doing so.
Upvotes: 0
Views: 2681
Reputation: 369
Why do you want to store it in a variable and do the conversion using PHP? If it's a datetime field just use the DATE_FORMAT function in the query to convert it right off the bat.
SELECT DATE_FORMAT(datetime, '%d/%b/%Y') datetime FROM table
Upvotes: 1
Reputation: 1454
If it is a datetime field (as it appears), just let MySQL give you the date as a timestamp:
SELECT UNIX_TIMESTAMP(my_date) As my_timestamp
Then you can use php to format the timestamp as you like:
$formatted_date = date("d m Y",$timestamp);
Regards
Upvotes: 2
Reputation: 324630
For any kind of date format conversion, first you convert the old date to a timestamp, then format the timestamp into a date.
Example:
$old = "6-2-1992 10:24 PM";
$new = date("d/M/Y",strtotime($old));
echo $new; // result: 06/Feb/1992
Just be very careful that you don't run afoul of the US format vs. UK format (ie. is 3/4 the 3rd of April or the 4th of March?)
Upvotes: 3