Skip to content

Instantly share code, notes, and snippets.

@mcloide
Last active April 11, 2016 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcloide/eb7db1cc38fe4c6476388902bda724c3 to your computer and use it in GitHub Desktop.
Save mcloide/eb7db1cc38fe4c6476388902bda724c3 to your computer and use it in GitHub Desktop.
PHP Datetime comparison unexpected behavior
<?php
foreach (['EST', 'EDT', 'CST', 'CDT', 'PST', 'PDT'] as $tz) {
echo "---- TZ: $tz ---- \n";
$now = new DateTime('@' . (new DateTime('11:30 AM EST'))->getTimeStamp());
$earlier = new DateTime('7 AM', new DateTimeZone($tz));
echo $now->format('r') . "\n";
echo $earlier->format('r') . "\n";
$mod = $now->setTimeZone(new DateTimeZone($tz));
echo $now->format('r') . "\n";
echo $earlier->format('r') . "\n";
echo (int)($earlier < $now) . "\n";
echo $now->getTimestamp() . "\n";
echo (int)($earlier < $now) . "\n";
echo "------------------\n\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment