I am retrieving date from MySQL in the format yyyy/mm/dd 00:00:00. I want to convert this date into format dd/MMM/yyyy in PHP.
Use PHP's date and strtotime:
$formatted = date('d/M/Y', strtotime($date_from_mysql));
Or use MySQL's built in DATE_FORMAT function:
SELECT DATE_FORMAT(datetime, '%d/%b/%Y') datetime FROM table
Or, you can mix a bit of both flavours:
SELECT UNIX_TIMESTAMP(datetime) timestamp FROM table;
$formatted = date('d/M/Y', $timestamp);
The last method is handy if you need to get several different formats on the same page; say, you would like to print the date and time separately, then you can just use date('d/M/Y', $timestamp) and date('H:i', $timestamp) without any further conversions.
Although you specifically asked for a php solution you might also be interested in MySQL's date_format() function.
SELECT date_format(dt, '%d/%m/%Y') ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With