Last active
December 17, 2015 02:28
-
-
Save dongilbert/5535775 to your computer and use it in GitHub Desktop.
DateTime weirdness. When passing a timestamp to the constructor, if it's a Unix timestamp, you must prepend it with an @.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
print_r(new DateTime('01/01/2012')); | |
/* | |
Good | |
DateTime Object | |
( | |
[date] => 2012-01-01 00:00:00 | |
[timezone_type] => 3 | |
[timezone] => America/Chicago | |
) | |
*/ | |
print_r(new DateTime(strtotime('01/01/2012'))); | |
/* | |
Bad | |
DateTime Object | |
( | |
[date] => 7600-05-07 13:25:39 | |
[timezone_type] => 3 | |
[timezone] => America/Chicago | |
) | |
*/ | |
print_r(new DateTime('@' . strtotime('01/01/2012'))); | |
/* | |
Finally Correct. | |
DateTime Object | |
( | |
[date] => 2012-01-01 06:00:00 | |
[timezone_type] => 1 | |
[timezone] => +00:00 | |
) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment