RKh
RKh

Reputation: 14161

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.

Upvotes: 4

Views: 20709

Answers (5)

Tatu Ulmanen
Tatu Ulmanen

Reputation: 124768

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.

Upvotes: 13

jensgram
jensgram

Reputation: 31508

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 ....

Upvotes: 0

dnagirl
dnagirl

Reputation: 20456

$d=date_create('2009-12-04 09:15:00');
echo $d->format('dd/M/yyyy');

Upvotes: 0

Neil Aitken
Neil Aitken

Reputation: 7854

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'])

Upvotes: 0

VolkerK
VolkerK

Reputation: 96159

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') ...

Upvotes: 5

Related Questions