1

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

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Susan
  • 11
  • 2

1 Answers1

3

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>";
}
vascowhite
  • 18,120
  • 9
  • 61
  • 77
  • @susan simply use $when->startTime as the input for DateTime, eg `$date = new DateTime($when->startTime);` See edit to my answer. – vascowhite Jan 12 '12 at 14:29
  • Thank you @vascowhite ! We are getting there... timedate now shows up as 19 January 2012: 22:00 - How do I convert that to say 10pm instead? – Susan Jan 13 '12 at 02:48
  • Just wound up trying `g:i A m/d/y` and it created: 10:00 PM 01/19/12, and that works for me! Thank you so much for your help! – Susan Jan 13 '12 at 03:19
  • @susan, if you find answers on SO helpful then the way to thank people for their efforts is by up voting/accepting their answers. It's all about [reputation](http://stackoverflow.com/faq#reputation) here :) – vascowhite Jan 13 '12 at 08:04