Skip links

DateTime createFromFormat without time

August 12, 2015 at 9:08 AM - by Freek Lijten - 22 comments

Tags:

This is just a quick word of warning. I recently had a bug where two DateTime objects that were created with the same date were inequal to eachother. It turned out that this was a difference between the normal __construct() and createFromFormat. This is actually documented behaviour but since people might be searching for this problem, this post might help.

tl;dr

When you use createFromFormat use a ! in front of your format to have all time parts set to 0 (Epoch technically, but then you should read :p )

$A = new DateTime('2015-08-10');
$B = DateTime::createFromFormat('Y-m-d', '2015-08-10');
var_dump($A == $B);

I expected the dumped result to be true. Both where created with the same date and without time after all. Oddly enough (for me, at that time) I noticed that the result of this was false. The object created with createFromFormat did have a time, viz. the current time. Run this code at 12:31 and the time will be 12:31 for object B.

Since the constructor does not add any time and defaults to all zeroes, it all of a sudden makes sense these two are not equal. This is documented behaviour as one can see here when you scroll to the format explanation. To make sure that the current time is not copied you must preceed your format with an exalamation mark. In this case the values from unix epoch (the start of unix time) will be copied. Since the time parts of unix epoch are all zeroes there effectively will be no time set:

// Without !
var_dump(DateTime::createFromFormat('Y-m-d', '2015-08-10'));

/**
object(DateTime)#1 (3) {
  ["date"]=>
  string(26) "2015-08-10 15:54:44.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(16) "Europe/Amsterdam"
}
*/

// With !
var_dump(DateTime::createFromFormat('!Y-m-d', '2015-08-10'));

/**
object(DateTime)#1 (3) {
  ["date"]=>
  string(26) "2015-08-10 00:00:00.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(16) "Europe/Amsterdam"
}
*/

Now this all not earh-shattering, but it surprised me and it might surprise others, I hope some of them might stumble upon this.

Share this post!

Comments

  1. Tristan BaileyTristan Bailey Wrote on August 19, 2015 at 9:27:21 AM

    Thanks for pointing this out, before when this was needed I was taking on the extra 00s to the creation call, concat with the var to be converted from a date. This makes more sense, though an extra ", true" or similar on the method might be more obvious.

  2. LaurentLaurent Wrote on April 5, 2016 at 10:48:53 AM

    Never saw that elsewhere, but precisely what I was looking for. Thanks a lot ! (though this should be added to PHP documentation !)

  3. Dennis PaulinoDennis Paulino Wrote on January 5, 2018 at 10:07:33 PM

    Thanks for this article, it was helpful! :)

  4. BrettBrett Wrote on March 26, 2018 at 8:35:48 PM

    Thanks, this was helpful.

  5. LuisLuis Wrote on May 4, 2018 at 2:39:59 PM

    Thanks! Your article saved my day :)

  6. JohnnyJohnny Wrote on July 11, 2018 at 10:12:53 AM

    Thanks! Very helpful information!

  7. salamsalam Wrote on January 15, 2019 at 11:32:44 PM

    Very Thanks, Helpful short tip.

  8. Maxim MandrikMaxim Mandrik Wrote on March 19, 2019 at 2:23:20 PM

    Big thanks!
    In my case it was extremely important.

  9. GithuaGithua Wrote on May 19, 2019 at 8:49:26 PM

    Thank you so much for this, i was facing problem implementing a date in a project, this was helpful

  10. HeidyHeidy Wrote on June 4, 2019 at 9:37:55 PM

    Thanks a lot

  11. David LucasDavid Lucas Wrote on August 21, 2019 at 6:45:56 PM

    Legend. I was pulling my hair out. Thank you

  12. seohero.ukseohero.uk Wrote on November 7, 2019 at 10:33:27 PM

    And the majority of them are using search engines.

  13. RhondaRhonda Wrote on December 20, 2019 at 12:10:30 AM

    Stained glass art auctions normally incluԀe things like lampshades.

  14. https://1seo.biz/https://1seo.biz/ Wrote on January 19, 2020 at 3:41:26 AM

    Inventive writing can Ьe Optimized for Search Engines.

  15. selling virginity auctionselling virginity auction Wrote on February 3, 2020 at 2:37:51 PM

    Many are loosig their virginity just casually.

  16. paris anal escortsparis anal escorts Wrote on February 3, 2020 at 9:45:49 PM

    Moѕt Paris escort girls ɑrе highly educated.

  17. https://1seo.bizhttps://1seo.biz Wrote on February 4, 2020 at 10:17:22 AM

    Search engine optimisation іs defined as Search engine optimization.

  18. VanceVance Wrote on February 13, 2020 at 4:34:20 PM

    In 2004, the employed bmw cars іn arizona a ѡhim.

  19. seo uk companyseo uk company Wrote on February 18, 2020 at 6:33:47 AM

    four. Social media enhances company trustworthiness.

  20. luxury escort in parisluxury escort in paris Wrote on February 26, 2020 at 1:23:42 AM

    Runs a high-class escortt agency қnown аs Elite Nites.

  21. buy bmw parts wholesalebuy bmw parts wholesale Wrote on February 26, 2020 at 2:31:17 AM

    You do not have any saved vehicles at this time.

  22. COMPANYCOMPANY Wrote on March 27, 2020 at 7:51:04 PM

    Attempt banner ads on search engines.

Leave a comment!

Italic and bold

*This is italic*, and _so is this_.
**This is bold**, and __so is this__.

Links

This is a link to [Procurios](http://www.procurios.nl).

Lists

A bulleted list can be made with:
- Minus-signs,
+ Add-signs,
* Or an asterisk.

A numbered list can be made with:
1. List item number 1.
2. List item number 2.

Quote

The text below creates a quote:
> This is the first line.
> This is the second line.

Code

A text block with code can be created. Prefix a line with four spaces and a code-block will be made.