How to change date format from DD / MM / YYYY to YYYY-MM-DD?

How to change date string format using PHP?

From: 06/16/2010
To:2010-06-16

+3
source share
3 answers
$date = "06/16/2010";
echo date('Y-m-d', strtotime($date)); // outputs 2010-06-16

Using the strtotime function .

+5
source

php -r 'echo date("Y-m-d", strtotime("06/16/2010"));'

0
source

You should use \DateTimeand get rid of the lines as soon as possible:

$date = DateTime::createFromFormat('m/d/Y', '06/16/2010'); // \DateTime object
echo $date->format('Y-m-d'); // 2010-06-16

More details:
http://php.net/manual/en/datetime.createfromformat.php

0
source

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


All Articles