So far I'm doing this to convert dates
$date = '01/1990';
date('Y-m-d', strtotime(str_replace('-', '/', $date . '/01')));
But I get back a boolean false; Any ideas?
So far I'm doing this to convert dates
$date = '01/1990';
date('Y-m-d', strtotime(str_replace('-', '/', $date . '/01')));
But I get back a boolean false; Any ideas?
Your str_replace is searching for -
and replacing with /
.
Here is a simplified solution that opts to not concatenate the day onto the date string.
$date = '01/1990';
$date = str_replace('/', '-', $date);
$date = date('Y-m-1', strtotime($date));
If you do want to concatenate your day onto the date string format as dd/mm/yyyy
$date = '01/1990';
$date = '01/' . $date;
$date = str_replace('/', '-', $date);
$date = date('Y-m-d', strtotime($date));
Replace $data
with $date
:
echo date('Y-m-d', strtotime(date('Y-d-m', strtotime('01/' . str_replace('-', '/', $date)))));
strtotime
accepts dd-mm-yyyy
and mm/dd/yyyy
(and some other formats like yyyy-mm-dd
). The only way to differentiate between dd-mm-yyyy
and mm/dd/yyyy
is the separator.
Since you start with a string that appears to be in mm/yyyy
format, the easiest solution would be to convert it to dd-mm-yyyy:
$date = '01/1990';
date('Y-m-d', strtotime('01-'.str_replace('/', '-', $date)));
And you also might want to check the DateTime class.