Skip to content

Instantly share code, notes, and snippets.

@EdwardIII
Created July 31, 2013 14:32
Show Gist options
  • Save EdwardIII/6122497 to your computer and use it in GitHub Desktop.
Save EdwardIII/6122497 to your computer and use it in GitHub Desktop.
<?
function _compare_dates($utc_date_str, $date_obj){
$date1 = DateTime::createFromFormat(DateTime::ISO8601, $utc_date_str);
$date2 = clone($date_obj);
print "--------------------------------------------------------------------------------\n";
var_dump("Non-UTC Date1:");
var_dump($date1);
var_dump("Non-UTC Date2:");
var_dump($date2);
$utc = new DateTimeZone('UTC');
$date1->setTimezone($utc);
$date2->setTimezone($utc);
$date1->setTime(0, 0, 0);
$date2->setTime(0, 0, 0);
var_dump("UTC Date1:");
var_dump($date1);
var_dump("UTC Date2:");
var_dump($date2);
var_dump("Are these the same?");
var_dump(($date1 == $date2));
print "--------------------------------------------------------------------------------\n\n";
return ($date1 == $date2);
}
@EdwardIII
Copy link
Author

string(14) "Non-UTC Date1:"
object(DateTime)#6 (3) {
  ["date"]=>
  string(19) "2013-08-09 00:00:00"
  ["timezone_type"]=>
  int(1)
  ["timezone"]=>
  string(6) "+02:00"
}
string(14) "Non-UTC Date2:"
object(DateTime)#5 (3) {
  ["date"]=>
  string(19) "2013-08-08 15:29:46"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(13) "Europe/London"
}
string(10) "UTC Date1:"
object(DateTime)#6 (3) {
  ["date"]=>
  string(19) "2013-08-08 00:00:00"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(3) "UTC"
}
string(10) "UTC Date2:"
object(DateTime)#5 (3) {
  ["date"]=>
  string(19) "2013-08-08 00:00:00"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(3) "UTC"
}
string(19) "Are these the same?"
bool(true)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment