d/m/y to Y-m-d php stringtotime not working correctly
date, explode, php
Solution
Use DateTime:
$datetime = DateTime::createFromFormat('d/m/y', '22/02/13');
echo $datetime->format('Y-m-d');
See it in action
Reference
- DateTime
Problem
I have a date string of the format `d/m/y` e.g. `22/02/13` I would like to echo this string as `Y-m-d` but using the following code, it is not working. ``` $d = '22/02/13'; echo date('Y-m-d', strtotime($d)); ``` Output: `1970-01-01` What am I doing wrong? Do I have to explode this by `/` as the delimiter and do it manually or is there a simpler / more elegant solution?