2

I ask here well knowing that there is other questions and answers on this and i have been going through them: Calculate number of hours between 2 dates in PHP

But i still can't find a solution to my problem. Here is the scenario: Im using the Google Calendar Api. I have a foreach() loop running, where it graps the dateTime() for the event. I want to get the differens from the start and end time. My code is as follows:

$neweventDateStr = new DateTime($event->start->dateTime);
$neweventDateEnd = new DateTime($event->end->dateTime);
$diff = $eventDateEnd->diff($neweventDateStr);

However i get the following error on the last line:

Fatal error: Call to a member function diff() on string

I have checked and both variables are recognised as dateTime Objects. Therefore i can't figure out why is giving me the error as listed above. Any clue as to why this is?

Note, the code:

$event->start->dateTime 

Returns the value of the date and time from the google server as a dateTime RFC 3339 format: source: https://developers.google.com/google-apps/calendar/v3/reference/events

It returns the values like so:

$event->start->dateTime = 2015-07-06T14:30:00+02:00 
$event->se->dateTime = 2015-07-06T15:30:00+02:00

I have tried to:

echo $neweventDateStr;

and i returns the error(as expected) that the variable couldn't be turned into a string since its a dateTime Object format.

Community
  • 1
  • 1
DJESMOND
  • 23
  • 4

1 Answers1

1

The problem is in your code:-

     $diff = $eventDateEnd->diff($neweventDateStr);

it should be :-

$diff = $neweventDateEnd->diff($neweventDateStr); // because no where $eventDateEnd is defined.

An example is as follows:-

<?php
 $neweventDateStr = new DateTime('2015-04-04 10:10:10');
 $neweventDateEnd = new DateTime('2015-04-10 10:10:10');
 $diff = $neweventDateEnd->diff($neweventDateStr); // instead of $eventDateEnd i write $neweventDateEnd
 print_r($diff);
 ?>

Output:-https://eval.in/393320

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98