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. How to do this?
$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') ...
You should have a look at the date()
function in PHP (docs).
print date('d/M/Y', $row['tstamp']);
This applies if your date field is a DATETIME
or TIMESTAMP
. Remeber to wrap the field in UNIX_TIMESTAMP()
(docs), e.g. SELECT ..., UNIX_TIMESTAMP(dateField) AS tstamp FROM ...
.
You can actually get mySQL to convert the date to a unix timestamp
SELECT UNIX_TIMESTAMP(field) as date_field
FROM table
Then you can pass that straight into PHPs date() function to get the format you want
date('d/M/Y', $row['date_field'])