3

i have m-d-Y date format but i want Y-m-d format using strtotime, but it doesn't work?

$date_time = '05-23-2017 01:00';
echo date('Y-m-d H:i:s', strtotime($date_time));

but it will give output "1970-01-01 01:00:00" instead of "2017-05-23 01:00:00"

Apurv Chaudhary
  • 1,672
  • 3
  • 30
  • 55
  • you can try it in your local system, it will not work, and it's not duplicate Question. – Apurv Chaudhary May 19 '17 at 11:43
  • How is PHP supposed to guess that this is supposed to be `m-d-Y` format? You need to make that explicit. See the duplicate for how to. – deceze May 19 '17 at 11:45

1 Answers1

4

Try this one:

$date_time = '05-23-2017 01:00';

$newdatetime = str_replace('-', '/', $date_time);

echo date('Y-m-d H:i:s', strtotime($newdatetime));

// Output: 2017-05-23 01:00:00

The strtotime documentation reads:

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.

Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59