在 PHP 5.4 如何将 2 小时添加到 UTC 日期变量(和 return UTC 格式化为 ical)

in PHP 5.4 How do I add 2 hours to a UTC date variable (and return UTC formatted for ical)

我有一个日期变量,我需要简单地添加 2 小时。 它需要 return UTC 日期格式如下: 20150921T172345Z

None 我看到的日期操作示例正是我需要的,我很难将我看到的转换为我的案例。

这是我现在无法使用的内容:

//This yields exactly what I need for start date:
$rightNowUTC = gmdate("Ymd\THis\Z"); 

//But trying to add 2 hours gives an error. So what is wrong with syntax?
//  Start by creating end date variable that i will add the time to.
$endDateForWelcome = gmdate("Ymd\THis\Z");

//  Set duration (per examples I've seen)
$Duration = new DateInterval('PT2H'); // 2 hour

//  Then add duration to the date.
$endDateForWelcome->add( $Duration );

我收到下一个错误:

PHP Fatal error: Call to a member function add() on a non-object.

此日期将作为活动结束日期写入 .ics 文件(发布日历文件)。所以需要 returning UTC。我看到一些代码使用

date(DATE_ICAL, strtotime($meeting->ends))

但我不知道如何使它适应我的需要。

提前致谢!

试试这个:

$rightNowUTC = gmdate("Ymd\THis\Z");  //time right now
$endDateForWelcome = gmdate("Ymd\THis\Z", strtotime('+ 2 hours')); 

$endDateForWelcome 是一个字符串所以你必须用 strtotime()

解析它

http://php.net/manual/en/function.strtotime.php

echo "Time Right Now: ". $rightNowUTC ."</br>";
echo "Time After Adding:".  $endDateForWelcome;