简体   繁体   English

在PHP 5.4中如何将2小时添加到UTC日期变量(并返回格式化为UTC的UTC)

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

I have a date variable that I need to simply add 2 hours to. 我有一个日期变量,只需将其添加2个小时即可。 It needs to return UTC date formatted like: 20150921T172345Z . 它需要返回格式为: 20150921T172345Z UTC日期。

None of the date manipulation examples I've seen are exactly what I need and I'm having a hard time converting what I've seen to my case. 我见过的所有日期操作示例都不是我真正需要的,并且我很难将我所看到的转换为案例。

Here's what I have now that isn't working: 这是我现在无法正常工作的内容:

//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 );

I get the next error: 我收到下一个错误:

PHP Fatal error: Call to a member function add() on a non-object. PHP致命错误:在非对象上调用成员函数add()。

This date will be written into an .ics file as an event end date (publishing a calendar file). 该日期将作为事件结束日期(发布日历文件)写入.ics文件。 So returning UTC is required. 因此,需要返回UTC。 I've seen some code using 我看过一些代码使用

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

But I don't know how to adapt that to my needs. 但是我不知道如何适应我的需求。

Thanks in advance! 提前致谢!

Try this: 尝试这个:

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

$endDateForWelcome is a string so you have to parse it with strtotime() $endDateForWelcome是一个字符串,因此您必须使用strtotime()进行解析

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM