如何从字符串日期获取时间戳与时区,PHP?

时间:2014-11-05 20:41:48

标签: php datetime

我想用以下格式创建ics开始和结束时间(UTC):

DTSTART:20141107T110000Z
DTEND:20141107T120000Z

我有以下对象作为输入:

"event": {
    "timezone": "GMT+2",
    "start_time": "2014-11-07 13:00:00", // client local time
    "duration": 30
     },

 function _calc_end_date($start, $duration) {
    $start_date = new DateTime($start, new DateTimeZone('UTC'));
    $end_date = $start_date->add(new DateInterval('PT'.$duration.'M'));
    $end_date_str = $end_date->format('Y-m-d H:i:s');
    return $end_date_str;
 }

 private function dateToCal($timestamp) {
    return GMDATE("Ymd\THis\Z", $timestamp);
}

 $timezone  = $event['timezone']; // ????       
 $start = strtotime($event['start_time'] . " UTC");
 $end_date = $this->_calc_end_date($event['start_time'], $event['duration']);
 $end = strtotime($end_date . " UTC");

所以我写道:

"DTSTART:" . $this->dateToCal( $start ) . $eol .
"DTEND:" . $this->dateToCal( $end ) . $eol .

此代码有效但我得到的结果有2个小时的差异。你可以注意到我没有进入TimeZone,因为我不知道如何。

$timezone  = $event['timezone']; // 

不知怎的,我需要解析" GMT + 2"并计算

$start = strtotime($event['start_time'] . " UTC");

与TZ。

请帮忙,

3 个答案:

答案 0 :(得分:1)

$date = DateTime::createFromFormat( "Y-m-d H:i:s O", $event['start_time'] . ' ' . $event['timezone'] );
$date->setTimezone( new DateTimeZone( 'GMT' ) );
$startDate = $date->format( "Ymd\THis\Z" );

$date->add( new DateInterval( 'PT' . $event['duration'] . 'M' ) );
$endDate = $date->format( "Ymd\THis\Z" );

答案 1 :(得分:0)

使用substring和indexof获取“+”之后的所有内容并将该小时数添加到该时间

答案 2 :(得分:0)

什么是源时区名称?这段代码适合我:

date_default_timezone_set('UTC');
echo date('Y-m-d H:i', strtotime("2014-11-07 13:00:00 Europe/Warsaw"));
相关问题