php - Trying to compare DateTime objects and its wrong, why? -
$dtstart=new datetime($item["start"]["datetime"]); $dtend =new datetime($item["end"]["datetime"]); $dtstgoogle= new datetime($event->start->datetime); $dtstgoogle->settimezone(new datetimezone("america/chicago")); $dtendgoogle=new datetime($event->end->datetime); $dtendgoogle->settimezone(new datetimezone("america/chicago")); $dtdiffstart=$dtstart->diff($dtstgoogle); $dtdiffend=$dtend->diff($dtendgoogle);
i using php 5.6.13 , 5.6.14 try compare them , result either true or false regardless of value.
if ($dtdiffend) { printf("<br> start date time different %d %d %d %d %d %d ",$dtdiffstart->m,$dtdiffstart->d,$dtdiffstart->y,$dtdiffstart->h,$dtdiffstart->m,$dtdiffstart->s);} incorrect if (!$dtdiffend) { same above} incorrect if ($dtstart!=$dtstartgoogle) {same above} incorrect if ($dtstart!==$dtstartgoogle) {same above} incorrect
either results values of 0 0 0 0 0 0 (always true) or none (always false) @ , both incorrect. have tried other crazy things same results.
i have tried can think of, know doing wrong?
this works, there has got simplier way:
if ((($dtdiffend->m!=0) || ($dtdiffend->d!=0) || ($dtdiffend->y!=0) || ($dtdiffend->h!=0) || ($dtdiffend->i!=0) || ($dtdiffend->s!=0)))
any advice appreciated.
$dtdiffstart=$dtstart->diff($dtstgoogle); $dtdiffend=$dtend->diff($dtendgoogle);
are 2 instances of dateinterval, it'll considered true.
if ($dtstart!=$dtendgoogle)
in case return true, 2 datetime instances different (different timezone)
if want check if $dtstart , $dtendgoogle different, do:
($dtstart->format('u') != $dtendgoogle->format('u'))
'u' used unix timestamp. can change format depending on needs.
Comments
Post a Comment