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?

2
  • possible duplicate of php strtotime not working Commented Oct 7, 2013 at 19:50
  • strtotime isn't infallible - if you know the format, you should use DateTime::createFromFormat (available from PHP 5.3.x and above) or similar. Commented Oct 7, 2013 at 19:51

1 Answer 1

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');
Sign up to request clarification or add additional context in comments.

2 Comments

For me, using DateTime doesn't solve this issue. Any other suggestion?
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.