tadoman tadoman - 1 year ago 49
PHP Question

Why does strtotime('2016-01-01') result to 1.1.2015?

There's been a reported bug in my application where I was using the strtotime function on the mysql date returned from DB. It all worked well until today when a bug occured.

When I run:

date('j.n.o', strtotime('2016-01-01'))

I get result "1.1.2015" for some reason.

But if I run

date('j.n.o', strtotime('2016-02-01'))

I get "1.2.2016" which is correct but I have no idea why the first example failed.

I could easily do a workaround to make this work properly but I'm wondering in particular why does this function return such strange results with this input? I've checked with php 5.3 and 7.0 and I get same results

Answer Source

Try using Y instead of o for the year

echo date('j.n.Y', strtotime('2016-01-01'));


o --- ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0)


Y --- A full numeric representation of a year, 4 digits