-1

strtotime() and DateTime::createFromFormat return wrong date

and

 date('m/d/y H:i A', strtotime('09/29/2018 4:15 PM')) 

return me 09/29/18 16:15 PM

Why is that, and how to fix it?

  • 4
    You're using `m` to mean both month and minutes - the latter needs `i` – iainn Sep 19 '18 at 16:40
  • Possible duplicate of [Convert one date format into another in PHP](https://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php) – Ümañg ßürmån Sep 19 '18 at 19:24

2 Answers2

1

Use i for minute

  $date = DateTime::createFromFormat('m/d/y H:i A', '09/30/18 10:00 AM');
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
1

You need to use i instead of m for month. In addition, you need to use h instead of H, to show the hours in the 12 hours format.

$date = DateTime::createFromFormat('m/d/y h:i A', '09/30/18 10:00 AM');
echo  $date->format('m/d/y h:i');

As you can see, m is already used for the month, so if you would have looked to the php.net documentation, you would have found that there are different letters for the hours.

Alberto
  • 674
  • 13
  • 25