Convert to dd / mmm / yyyy

I get the date from MySQL in the format yyyy / mm / dd 00:00:00. I want to convert this date to dd / MMM / yyyy format in PHP.

+3
source share
5 answers

Use PHP date and strtotime :

$formatted = date('d/M/Y', strtotime($date_from_mysql));

Or use MySQL built in DATE_FORMAT :

SELECT DATE_FORMAT(datetime, '%d/%b/%Y') datetime FROM table

Or, you can mix a little of both flavors:

SELECT UNIX_TIMESTAMP(datetime) timestamp FROM table;

$formatted = date('d/M/Y', $timestamp);

The last method is convenient if you need to get several different formats on one page; let's say you want to print the date and time separately, then you can just use the date ('d / M / Y', $ timestamp) and the date ('H: i', $ timestamp) without any further conversion.

+11

, php, MySQL date_format().

SELECT date_format(dt, '%d/%m/%Y') ...
+5

date() PHP (docs).

print date('d/M/Y', $row['tstamp']);

This applies if your date field is DATETIMEor TIMESTAMP. Repeat to wrap the field in UNIX_TIMESTAMP()( docs ), for example. SELECT ..., UNIX_TIMESTAMP(dateField) AS tstamp FROM ....

0
source

In fact, you can force mySQL to convert the date to a unix timestamp

SELECT UNIX_TIMESTAMP(field) as date_field
FROM table

Then you can pass this directly to PHP's date () function to get the format you want

date('d/M/Y', $row['date_field'])
0
source
$d=date_create('2009-12-04 09:15:00');
echo $d->format('dd/M/yyyy');
0
source

Source: https://habr.com/ru/post/1724735/


All Articles