Zend Google Calendar API - 如何更改DateTime格式? [英] Zend Google Calendar API - How to Change DateTime Format?

查看:394
本文介绍了Zend Google Calendar API - 如何更改DateTime格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Zend Framework Gdata作为Google Calendar API和RFC3339中的datetime输出(如下所示:2012-01-19T22:00:00.000-08:00)。我需要添加什么PHP代码才能将其转换为UTC,使其看起来像这样:2012年1月19日晚上8点至晚上11点?我已经找到将RFC日期转换为字符串的php代码,但是我不太了解php可以更改代码以使用Zend Gdata公式...如下面的代码中所示,date 被称为何时...所以任何代码将不得不连接这两个不知何故...任何帮助是赞赏!

 <?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 ='my@email.com';
$ pass ='mypassword';
$ service = Zend_Gdata_Calendar :: AUTH_SERVICE_NAME; //日历的预定义服务名称

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

$查询= $ service-> newEventQuery();
//设置不同的查询参数
$ query-> setUser('mycalendarID');
$ query-> setVisibility('private');
$ query-> setProjection('full');
$ query-> setOrderby('starttime');
//从哪里获取事件的开始日期
$ query-> setStartMin('2012-01-01');
//结束日期
$ query-> setStartMax('2050-03-15');


//获取事件列表
try {
$ eventFeed = $ service-> getCalendarEventFeed($ query);
} catch(Zend_Gdata_App_Exception $ e){
echoError:。 $ e-> getMessage();
}
echo< ul>;
foreach($ eventFeed as $ event){
echo< tr>;
foreach($ event-> when as $ when){
echo< td> 。 $ when-> startTime。 < / td>;
}
echo< td> 。 $ event->内容。 < / td>;
$ where = $ event-> Where;
foreach($ where as $ eventplace)
{
echo< td> 。 $ eventplace。 < / td>;
}

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






感谢您的这个信息@vascowhite。 p>

两个问题:


  1. 输出结果如下:
    string(23)2012年1月20日:06:00


  2. 我从我的谷歌日历中拉出这个信息,它正在输出到我的HTML页面...我不是没有通过这个php创建新的事件...所以这个代码只是转换你写的日期,它没有转换我的谷歌日历事件日期从这个代码:


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



你知道怎么做吗?



谢谢,
苏珊

解决方案

您可以使用PHP的 DateTime 类可以很容易地执行。



首先从你的时间字符串创建一个DateTime对象: p>

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

由于字符串的-08:00部分,该对象位于正确的时区。
现在决定要转换到哪个时区并创建一个 DateTimeZone 对象(我特别提到UTC时选择了UTC): -

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

您的DateTime对象现已转换为UTC时区。

您可以使用 DateTime :: format()方法: -

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

输出:


2012年1月20日:06:00


适合您的代码: -

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


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

解决方案

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>";
}

这篇关于Zend Google Calendar API - 如何更改DateTime格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆