Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert to dd/mmm/yyyy

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.

like image 916
RKh Avatar asked Dec 03 '22 06:12

RKh


2 Answers

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.

like image 119
Tatu Ulmanen Avatar answered Dec 22 '22 15:12

Tatu Ulmanen


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') ...
like image 34
VolkerK Avatar answered Dec 22 '22 14:12

VolkerK