繁体   English   中英

活动开始时间和结束时间不显示-带有google-api-php-client的Google日历

[英]Event start time and end time does not show - Google Calendar with google-api-php-client

<?php

session_start();
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_CalendarService.php';

$client = new Google_Client();
$client->setApplicationName("Google Calendar PHP Starter Application");

$client->setUseObjects(true);
$cal = new Google_CalendarService($client);

if (isset($_GET['logout'])) {
  unset($_SESSION['token']);
}

if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}

$service = new Google_CalendarService($client);
if ($client->getAccessToken()) {
    $_SESSION['token'] = $client->getAccessToken();
}
//$rightNow = date('c');
//$params = array('singleEvents' => 'true', 'orderBy' => 'startTime', 'timeMin' => $rightNow);
$rightNow = date('c');

$tomorrow_in_seconds = 60 * 60 * 24;
$tomorrow = date('yyyy-mm-dd', date("Y-m-d\Th:i:s", time() + $tomorrow_in_seconds));

$params = array('singleEvents' => 'true', 'orderBy' => 'startTime', 'timeMin' => $rightNow, 'timeMax' => $tomorrow);
$events = $service->events->listEvents('primary', $params);

foreach ($events->getItems() as $event) {
        echo $event->getLocation()."<br/>";
        echo $event->when[0]->startTime;
        echo $event->when[0]->endTime;

        echo '-----<br />';
}


?>

我知道了活动的地点。 但是无法检索startTime和endTime。 还有没有办法只显示今天的事件? 最终,我想在startTime和当前时间相同时在我的网站上显示一个弹出窗口。 那么有人可以帮助我获得StartTime吗?

我在这里查看过: https : //developers.google.com/google-apps/calendar/v3/reference/events/list但它并不能告诉您如何获取开始时间和结束时间。 这确实是必要的。 PHP不是我的强项。 那有人可以帮我吗?

谢谢。


经过长时间的研究,我能够提取信息。 但是只能用Zend Framework做到。 希望这对其他人有帮助。

require_once 'Zend/Loader.php';
    Zend_Loader::loadClass('Zend_Gdata');
    Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
    Zend_Loader::loadClass('Zend_Gdata_Calendar');
    Zend_Loader::loadClass('Zend_Http_Client');

    // create authenticated HTTP client for Calendar service
    $gcal = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
    $user = "your-email@gmail.com";
    $pass = "your-password";
    $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $gcal);
    $gcal = new Zend_Gdata_Calendar($client);

    // generate query to get event list
    $query = $gcal->newEventQuery();
    $query->setUser('default');
    $query->setVisibility('private');
    $query->setProjection('basic');

    // get and parse calendar feed
    // print output
    try {
      $feed = $gcal->getCalendarEventFeed($query);
    } catch (Zend_Gdata_App_Exception $e) {
      echo "Error: " . $e->getResponse();
    }
    ?>
     <h1><?php echo $feed->title; ?></h1> <!-- displays email title -->
    <ol>

    <?php       



        $today = date('yyyy-mm-dd');
        $tomorrow = date(DateTime::ATOM, time()+(1 * 24 * 60 * 60));

        $gdataCal = new Zend_Gdata_Calendar($client);
        $query = $gdataCal->newEventQuery();
        $query->setUser('default');
        $query->setVisibility('private');
        $query->setProjection('full');
        $query->setOrderby('starttime');
        $query->setStartMin(date('Y-m-d 00:00:00'));
        $query->setStartMax(date('Y-m-d 23:59:59'));

        // echo date('Y-m-d G:i:s');

        $timestr = '2012-01-19T22:00:00.000-08:00';
        $date = new DateTime($timestr);

        // var_dump($date->format('d F  Y: H:i'));

        $eventFeed = $gdataCal->getCalendarEventFeed($query);
        echo "<ul>";
        echo "<li>";
        foreach ($eventFeed as $event) {

                echo "<ul>";

                foreach ($event->when as $when) {

                        echo "<li>Title: " . $event->title->text . "</li>";
                        echo "<li>Location: ". $event->where[0]."</li>";
                        echo "<li>Content: ".$event->content."</li>" ;
                        $startDateTime = new DateTime($when->startTime);
                        $endDateTime = new DateTime($when->endTime);
                        echo "<li>Starts: " . $startDateTime->format('d F  Y: H:i'). "</li>";
                        echo "<li>Ends: " . $endDateTime->format('d F  Y: H:i'). "</li>";
                }

                echo "</ul>";
                echo "</li>";
      }

      echo "</ul>";

抱歉,代码混乱-我仍在处理未解决的部分。 但是事件检索正在起作用...

这是输出!

Title: Test event 4
Location: International
Content: Testing Email
Starts: 06 June 2013: 21:00
Ends: 06 June 2013: 22:00

我刚刚通过本机Client PHP API找到了解决方案

foreach ($events->getItems() as $event) {
    echo $event->getStart()->getDateTime();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM