Converting string to Date and DateTime

Convert timestamp to readable date/time PHP
PHP file handling read and write

solution 1.

Use strtotime() on your first date then date('Y-m-d') to convert it back:

$time = strtotime('10/16/2003');

$newformat = date('Y-m-d',$time);

echo $newformat;
// 2003-10-16

Note: There is a difference between using forward slash / and hyphen - in the strtotime() function.

solution 2

Observe the difference between m/d/Y and m-d-Y formats.

PHP considers / to mean m/d/Y and - to mean d-m-Y. I would explicitly describe the input format in this case:

$ymd = DateTime::createFromFormat('m-d-Y', '10-16-2003')->format('Y-m-d');

https://stackoverflow.com/questions/6238992/converting-string-to-date-and-datetime

Convert timestamp to readable date/time PHP
PHP file handling read and write
en English
X
Scroll to Top