DateTime createFromFormat without time
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.
Comments
-
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.
-
Never saw that elsewhere, but precisely what I was looking for. Thanks a lot ! (though this should be added to PHP documentation !)
-
Thanks for this article, it was helpful! :)
-
Thanks, this was helpful.
-
Thanks! Your article saved my day :)
-
Thanks! Very helpful information!
-
Very Thanks, Helpful short tip.
-
Big thanks!
In my case it was extremely important. -
Thank you so much for this, i was facing problem implementing a date in a project, this was helpful
-
Thanks a lot
-
Legend. I was pulling my hair out. Thank you
-
And the majority of them are using search engines.
-
Stained glass art auctions normally incluԀe things like lampshades.
-
Inventive writing can Ьe Optimized for Search Engines.
-
Many are loosig their virginity just casually.
-
Moѕt Paris escort girls ɑrе highly educated.
-
Search engine optimisation іs defined as Search engine optimization.
-
In 2004, the employed bmw cars іn arizona a ѡhim.
-
four. Social media enhances company trustworthiness.
-
Runs a high-class escortt agency қnown аs Elite Nites.
-
You do not have any saved vehicles at this time.
-
Attempt banner ads on search engines.