【问题标题】:Zend Google Calendar API - How to Change DateTime Format?Zend Google Calendar API - 如何更改日期时间格式?
【发布时间】:2012-02-05 15:25:51
【问题描述】:

我将 Zend Framework Gdata 用于 Google 日历 API 和 RFC3339 中的日期时间输出(如下所示:2012-01-19T22:00:00.000-08:00)。我需要添加什么 php 代码才能将其转换为 UTC,使其看起来像这样:2012 年 1 月 19 日 8pm-11pm ?我找到了将 RFC 日期转换为字符串的 php 代码,但我对 php 的了解还不够,无法更改代码以使用 Zend Gdata 公式……正如您将在下面的代码中看到的那样,“日期" 被称为“何时”...所以任何代码都必须以某种方式连接这两者...感谢任何帮助!

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

感谢您提供此信息@vascowhite。

两个问题:

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

  2. 我正在从我的谷歌日历中提取此信息,并将其输出到我的 html 页面中...我不是通过此 php 创建新事件...所以此代码只是转换了您编写的日期,它没有转换从这段代码中提取的我的谷歌日历事件日期:

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

你知道怎么做吗?

谢谢你, 苏珊

【问题讨论】:

  • 你为什么要把 tr's & td's 放在一个列表中?日历在哪个时区?

标签: zend-framework datetime google-calendar-api gdata rfc3339


【解决方案1】:

您可以使用 PHP 的 DateTime 类很容易地做到这一点。

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

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

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

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

您的 DateTime 对象现已转换为 UTC 时区。
您可以使用DateTime::format() 方法将其转换为您想要的格式:-

var_dump($date->format('d F  Y: 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>";
}

【讨论】:

  • @susan 只需使用 $when->startTime 作为 DateTime 的输入,例如 $date = new DateTime($when-&gt;startTime); 请参阅我的答案的编辑。
  • 谢谢@vascowhite!我们到了...时间日期现在显示为 2012 年 1 月 19 日:22:00 - 我如何将其转换为晚上 10 点?
  • 刚结束尝试 g:i A m/d/y 并创建:10:00 PM 01/19/12,这对我有用!非常感谢您的帮助!
  • @susan,如果您发现关于 SO 的答案很有帮助,那么感谢人们努力的方法就是投票/接受他们的答案。这都是关于reputation 这里:)
猜你喜欢
  • 1970-01-01
  • 2022-08-19
  • 2018-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-29
  • 2015-12-27
  • 1970-01-01
相关资源
最近更新 更多