Zend Google Calendar API - How to Change DateTime Format?

☆樱花仙子☆ 提交于 2019-12-11 05:39:16

问题


I am using the Zend Framework Gdata for the Google Calendar API and the datetime outputs in RFC3339 (which looks like this: 2012-01-19T22:00:00.000-08:00). What php code do I need to add in order to convert that to UTC so that it looks like this: January 19, 2012 8pm-11pm ? I have found php code that converts RFC dates to string, but I don't know enough about php to be able to alter the code to work with the Zend Gdata formulas... as you will see in my code below, the "date" is referred to as "when"... so any code will have to connect those two somehow... any help is appreciated!

<?php
$path = '/home/ZendGdata/library';
$oldPath = set_include_path(get_include_path() . PATH_SEPARATOR . $path);

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

// User whose calendars you want to access
$user = 'my@email.com';
$pass = 'mypassword';
$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME; // predefined service name for calendar

$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$service = new Zend_Gdata_Calendar($client);

$query = $service->newEventQuery();
// Set different query parameters
$query->setUser('mycalendarID');
$query->setVisibility('private');
$query->setProjection('full');
$query->setOrderby('starttime');
// Start date from where to get the events
$query->setStartMin('2012-01-01');
// End date
$query->setStartMax('2050-03-15');


// Get the event list
try {
    $eventFeed = $service->getCalendarEventFeed($query);
} catch (Zend_Gdata_App_Exception $e) {
    echo "Error: " . $e->getMessage();
}
echo "<ul>";
foreach ($eventFeed as $event) {
echo "<tr>";
    foreach ($event->when as $when) {
      echo "<td>" . $when->startTime . "</td>";
    }
    echo "<td>" . $event->content . " </td>";
    $where=$event->Where;
    foreach($where as $eventplace)
    {
        echo "<td>" . $eventplace . " </td>";
    }

}
echo "</tr>";
echo "</ul>";
?>

Thank you for this information @vascowhite.

Two issues:

  1. The output turned out like this: string(23) "20 January 2012: 06:00"

  2. I am pulling this info from my google calendar, and it is outputting into my html page...I am not not creating new events through this php...so this code simply converted the date that you wrote, it didn't convert my google calendar event date which is pulled from this code:

foreach ($event->when as $when) { echo "<td>" . $when->startTime . "</td>"; }

Do you know how to do that?

Thank you, Susan


回答1:


You can use PHP's DateTime class to do this quite easily.

First create a DateTime object from your time string:-

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

That object is in the correct time zone because of the '-08:00' part of the string. Now decide which time zone you want to convert to and create a DateTimeZone object (I have chosen UTC as you specifically mention it):-

$tz = new DateTimeZone('UTC');
$date->setTimezone($tz);

Your DateTime object has now been converted to the UTC time zone.
You can get it into your desired format by using the DateTime::format() method:-

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

Output:

20 January 2012: 06:00

To fit into your code:-

foreach ($event->when as $when) {
  $date = new DateTime($when->startTime);
  echo "<td>" . $date->format('d F  Y: H:i') . "</td>";
}


来源:https://stackoverflow.com/questions/8782577/zend-google-calendar-api-how-to-change-datetime-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!