3

With the help of this question, I have been able to parse a date string into a DateTime object, then display it in the desired format. The DateTime constructor works well in deciphering DateTime strings with one (very understandable) exception - it cannot tell the difference between dd/mm/YYYY and mm/dd/YYYY.

If I do the following:

$dt = new DateTime("05/03/1900"); // 5th march 1900
echo $dt->format('Y-m-d');

Then I get the following output:

1900-05-03

However, the original date is in dd/mm/YYYY format, so my output is incorrect - I now have the 3rd May 1900.

Is there a way to set DateTime to prefer dd/mm/YYYY over mm/dd/YYYY when parsing a date string?

Edit: I am calling date_default_timezone_set('Europe/London'); in the constructor of this class but this doesn't seem to have any effect. Perhaps this isn't linked to the DateTime class.

8
  • 2
    For timezone: DateTime::createFromFormat( 'd/m/Y', '05/03/1900' , new DateTimeZone('Europe/London')); Commented Jun 8, 2016 at 14:41
  • 1
    The DateTime object is like strtotime() It assumes the date is USA format if you use / as a seperator and European if you use - as a seperator. Commented Jun 8, 2016 at 14:42
  • If the format is unknown, then you can NOT reliably parse it. what's to say that your date is dd/mm/yyyy or mm/dd/yyyy? The / hints that it's a US format, but nothing says it HAS to be. that's either march 5th, or may 3rd. you cannot tell. Commented Jun 8, 2016 at 14:45
  • @MarcB I am instructing the user that the timestamp should be dd/mm/yyyy. My versions of chrome and firefox have slightly different date inputs and provide me date strings in different formats. Therefore I need to handle both. Chrome, I know for sure will be dd/mm/yyyy. Firefox, on the other hand, will be yyyy/mm/dd, which I need to parse and convert. It is this parsing process that is causing confusion. Commented Jun 8, 2016 at 14:46
  • @lolka_bolka Using DateTime::createFromFormat does not seem to return a DateTime value. When I then try and call format(), I get the following error: Fatal error: Call to a member function format() on boolean Commented Jun 8, 2016 at 14:50

1 Answer 1

4

You can use the DateTime::createFromFormat

$dt = DateTime::createFromFormat( 'd/m/Y', '05/03/1900' );
echo $dt->format( 'Y-m-d' );
Sign up to request clarification or add additional context in comments.

5 Comments

Add the timezone please.
This was actually my first attempt before using the DateTime constructor. However, this seems to return a boolean instead of a DateTime object, as I then get the error: Fatal error: Call to a member function format() on boolean
@BrianButterfield If you are getting a boolean it means the function is failing. Double check the formatting you are passing in with the date you are passing in to ensure they are the same format.
What about the "when format is unknown" requirement in the title?
This will only work when you know what the format being passed is.

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.