Why is strtotime() giving the wrong date?

anhduc.bkhn picture anhduc.bkhn · Jan 3, 2014 · Viewed 7.1k times · Source

I'm using the following code

$t = strtotime('Saturday, 28 Dec, 2013');
echo date('d/m/Y H:i',$t);
// output : 03/01/2015 20:13

$t = strtotime('Wednesday, 01 Jan, 2014');
echo date('d/m/Y H:i',$t);
// output: 01/01/2014 20:14

The first example prints out the wrong date, and the second one prints out the correct date.

Why is this and how do I fix it?

Answer

Niet the Dark Absol picture Niet the Dark Absol · Jan 3, 2014

strtotime only understands a specific set of formats. If your input is not in one of these formats, it will do its best to guess, but as you can see the results can vary.

Try this:

$t = date_create_from_format("D, d M, Y","Saturday, 28 Dec, 2013");
echo date_format($t,"d/m/Y H:i");