user1156168
user1156168

Reputation: 956

php strtotime return wrong timestamp

I'm using php strtotime function (date format Day, month and two digit year, with dots or tabs d.m.y from doc: http://www.php.net/manual/en/datetime.formats.date.php) and found problem:

From php.net example strtotime("30.6.08") -> 1214773200 -> Sun, 29 Jun 2008 21:00:00 GMT(reverse convert right)

another variant strtotime("24.10.13") -> 1381180213 -> Mon, 07 Oct 2013 21:10:13 GMT (reverse is not right)

but strtotime("30.10.13") -> 1383084000 -> Tue, 29 Oct 2013 22:00:00 GMT (reverse result again correctly)

Whats wrong?

Upvotes: 1

Views: 2813

Answers (1)

Pitchinnate
Pitchinnate

Reputation: 7556

Strtotime is guessing what format you are sending the date in as and guessing wrong. You should use DateTime() where you can specify the format you are sending the date in as.

$date = DateTime::createFromFormat('d.m.y', $datestring);
echo $date->format('Y-m-d');

Upvotes: 4

Related Questions