0

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?

Dingo Egret
  • 15
  • 1
  • 5

3 Answers3

1

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));
tchow002
  • 1,068
  • 6
  • 8
0

Replace $data with $date:

echo date('Y-m-d', strtotime(date('Y-d-m', strtotime('01/' . str_replace('-', '/', $date)))));
stealthyninja
  • 10,343
  • 11
  • 51
  • 59
0

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.

Arjan
  • 9,784
  • 1
  • 31
  • 41