1

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?

user1156168
  • 936
  • 13
  • 32
  • possible duplicate of [php strtotime not working](http://stackoverflow.com/questions/5680056/php-strtotime-not-working) – John Parker Oct 07 '13 at 19:50
  • strtotime isn't infallible - if you know the format, you should use [DateTime::createFromFormat](http://www.php.net/manual/en/datetime.createfromformat.php) (available from PHP 5.3.x and above) or similar. – John Parker Oct 07 '13 at 19:51

1 Answers1

4

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');
Pitchinnate
  • 7,517
  • 1
  • 20
  • 37
  • For me, using DateTime doesn't solve this issue. Any other suggestion? – Alexandre Bourlier Jul 08 '14 at 16:24
  • I eventually solved it. THe client database was actually sending me incorrect dates such as "00-00-2014". That is why I was having strange behaviour, but PHP was doing the job correctly, the problem was between the chair and the desk – Alexandre Bourlier Jul 09 '14 at 08:15