Converting string to Date and DateTime

C
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

1 comment

Tutor Network

Share this

Learn PHP from A to Z