setApplicationName(APPLICATION_NAME); $client->setScopes(SCOPES); $client->setAuthConfigFile(CLIENT_SECRET_PATH); $client->setAccessType('offline'); // Load previously authorized credentials from a file. $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH); if (file_exists($credentialsPath)) { $accessToken = file_get_contents($credentialsPath); } else { // Request authorization from the user. $authUrl = $client->createAuthUrl(); printf("Open the following link in your browser:\n%s\n", $authUrl); print 'Enter verification code: '; $authCode = trim(fgets(STDIN)); // Exchange authorization code for an access token. $accessToken = $client->authenticate($authCode); // Store the credentials to disk. if(!file_exists(dirname($credentialsPath))) { mkdir(dirname($credentialsPath), 0700, true); } file_put_contents($credentialsPath, $accessToken); printf("Credentials saved to %s\n", $credentialsPath); } $client->setAccessToken($accessToken); // Refresh the token if it's expired. if ($client->isAccessTokenExpired()) { $client->refreshToken($client->getRefreshToken()); file_put_contents($credentialsPath, $client->getAccessToken()); } return $client; } /** * Expands the home directory alias '~' to the full path. * @param string $path the path to expand. * @return string the expanded path. */ function expandHomeDirectory($path) { $homeDirectory = getenv('HOME'); if (empty($homeDirectory)) { $homeDirectory = getenv("HOMEDRIVE") . getenv("HOMEPATH"); } return str_replace('~', realpath($homeDirectory), $path); } // Get the API client and construct the service object. $client = getClient(); $service = new Google_Service_Calendar($client); // Print the next 10 events on the user's calendar. $calendarId = 'primary'; $optParams = array( 'maxResults' => 1, 'orderBy' => 'startTime', 'singleEvents' => TRUE, 'timeMin' => date('c'), ); $results = $service->events->listEvents($calendarId, $optParams); if (count($results->getItems()) == 0) { print "No upcoming events found.\n"; } else { //print "Next event:\n"; foreach ($results->getItems() as $event) { $start = $event->start->dateTime; if (empty($start)) { $start = $event->start->date; } //echo getTimeinHoursToEvent($event); /// echo "
"; echo '<'.getTimeinHoursToEvent($event).'>'; } //echo "
"; // echo "The time is " . date("h:i:sa"); } function getTimeinHoursToEvent($event) { $event_start_time = new DateTime($event->start->dateTime); $current_time = new DateTime(); $hours_to_event = (($event_start_time->getTimestamp() - $current_time->getTimestamp())/60); return round($hours_to_event); }