Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

date

php

format

How to change format of date string using PHP?

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

like image 595
EzzDev Avatar asked Dec 27 '25 16:12

EzzDev


2 Answers

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

Using the strtotime function.

like image 104
Russell Dias Avatar answered Dec 31 '25 17:12

Russell Dias


You should use \DateTime and get rid of strings as soon as possible:

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

See more:
http://php.net/manual/en/datetime.createfromformat.php

like image 33
luchaninov Avatar answered Dec 31 '25 19:12

luchaninov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!