0

I am converting date time into different formate but that code gives me wrong date time. I have show my code below

echo date('M d, Y h:i A', strtotime('25/05/19 05:59 AM')); die();

this code shows

Dec 31, 1969 06:00 PM

which is wrong can anybody help me in this.

  • Possible duplicate of [Strtotime() doesn't work with dd/mm/YYYY format](https://stackoverflow.com/questions/2891937/strtotime-doesnt-work-with-dd-mm-yyyy-format) – Zeitounator May 25 '19 at 11:55

3 Answers3

0

Try this:

echo date('M d, Y h:i A', strtotime(str_replace('/', '-', '19/05/25 05:59 AM')));

Test it here: http://sandbox.onlinephpfunctions.com/code/73d92119a368316a7c8d5af521319902c157b537

Amit Merchant
  • 1,045
  • 6
  • 21
0

From the strtotime manual:

Note:

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. If, however, the year is given in a two digit format and the separator is a dash (-), the date string is parsed as y-m-d.

So your date is invalid. You can work around this using date_create_from_format instead:

$date = date_create_from_format('d/m/y h:i A', '25/05/19 05:59 AM');
echo $date->format('M d, Y h:i A');

Output:

May 25, 2019 05:59 AM

Demo on 3v4l.org

Nick
  • 138,499
  • 22
  • 57
  • 95
  • a dash (-) signifies European D-M-Y and a period (.) signifies ISO Y.M.D. (-) and (.) both are different format. – Rasa Mohamed May 25 '19 at 11:15
  • @RasaMohamed what does that have to do with my answer? – Nick May 25 '19 at 11:19
  • minor change in your answer that's why commented for this `if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed` – Rasa Mohamed May 25 '19 at 11:25
  • @RasaMohamed the text is copied verbatim from the PHP manual; it's what the behaviour of `strtotime` is. If that happens to be different from other interpretations of `-` and `.`, that's not really relevant to the question. – Nick May 25 '19 at 11:28
  • Yes I too referred from [php](https://www.php.net/manual/en/function.strtotime.php) if my first comment is not relevant then this `if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed` should not be in your answer RIGHT!. I don't want to drag this comments for silly. – Rasa Mohamed May 25 '19 at 11:36
0

Use this

echo date('M d, Y h:i A', strtotime('19-05-25 05:59 AM')); die();
Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20