Skip to content

Instantly share code, notes, and snippets.

@dongilbert
Last active December 17, 2015 02:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dongilbert/5535775 to your computer and use it in GitHub Desktop.
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 @.
<?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