1
 $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 am using PHP 5.6.13 and now 5.6.14 Then I try to compare them and the result is either always true or always false regardless of the value.

  if ($dtDiffEnd) { printf("<br> Start Date time are different %d %d %d  %d %d %d ",$dtDiffStart->m,$dtDiffStart->d,$dtDiffStart->y,$dtDiffStart->h,$dtDiffStart->m,$dtDiffStart->s);}   incorrect
  if (!$dtDiffEnd) { same as above} incorrect
  if ($dtStart!=$dtStartGoogle) {same as above} incorrect
  if ($dtStart!==$dtStartGoogle) {same as above} incorrect

Either I get results with values of 0 0 0 0 0 0 (always true) or none (always false) at all and both are incorrect. I have tried other crazy things with the same results.

I have tried everything I can think of, anyone know what I am doing wrong?

This works, but there has got to be a simplier way:

if ((($dtDiffEnd->m!=0) || ($dtDiffEnd->d!=0) || ($dtDiffEnd->y!=0) || ($dtDiffEnd->h!=0) || ($dtDiffEnd->i!=0) || ($dtDiffEnd->s!=0)))

any advice appreciated.

1 Answer 1

0
$dtDiffStart=$dtStart->diff($dtStGoogle);
$dtDiffEnd=$dtEnd->diff($dtEndGoogle);

Are 2 instances of DateInterval, so it'll always be considered as true.

if ($dtStart!=$dtEndGoogle)

In your case it will always return true, as your two DateTime instances are different (different timezone)

If you just want to check if $dtStart and $dtEndGoogle are different, just do:

($dtStart->format('U') != $dtEndGoogle->format('U'))

'U' is used to get the unix timestamp. You can change the format depending on your needs.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.