Php 是否通过DateTime类正确设置文件的日期字符串格式?

Php 是否通过DateTime类正确设置文件的日期字符串格式?,php,icalendar,Php,Icalendar,iCalender文件要求其文件中的DTSTART和DTEND参数采用以下格式: 20140715T035959Z 基本上,长格式的年、两位数的月、两位数的日,字母“T”将日期从时间中打断,然后是两位数的小时、分钟、秒等,再加上字母“Z” 我目前有以下PHP格式的日期: Y-m-d H:i:s 我目前正在尝试使用DateTime::format方法将其格式化为iCalendar接受的字符串,我认为这可能会起作用: format('Ymd\THis\Z'); 我对字符T和Z进行了转义,希望它

iCalender文件要求其文件中的
DTSTART
DTEND
参数采用以下格式:

20140715T035959Z
基本上,长格式的年、两位数的月、两位数的日,字母“T”将日期从时间中打断,然后是两位数的小时、分钟、秒等,再加上字母“Z”

我目前有以下PHP格式的日期:

Y-m-d H:i:s
我目前正在尝试使用
DateTime::format
方法将其格式化为iCalendar接受的字符串,我认为这可能会起作用:

format('Ymd\THis\Z');
我对字符T和Z进行了转义,希望它们能够出现,但当我的事件被回响到文件中时,它只是空的。我感觉我对iCal日期时间格式的表示不正确。想法

当前iCal代码:

DTSTART:".$calData->eventStart()."
public function eventStart() {
    $inputDateTime = $this->details['date_time'];

    // Convert MySQL datetime to ical datetime
    $temp = DateTime::createFromFormat('Y-m-d H:i:s', $inputDateTime);
    $eventStart = $temp->format('Ymd\THis\Z');

    echo $eventStart; // This should be RETURN, not ECHO! 
}
当前
$calData->eventStart()
code:

DTSTART:".$calData->eventStart()."
public function eventStart() {
    $inputDateTime = $this->details['date_time'];

    // Convert MySQL datetime to ical datetime
    $temp = DateTime::createFromFormat('Y-m-d H:i:s', $inputDateTime);
    $eventStart = $temp->format('Ymd\THis\Z');

    echo $eventStart; // This should be RETURN, not ECHO! 
}

答复:
是的,所以这不是问题。我只是
回显日期时间,而不是
返回日期时间

你可以试试这样的

<?php
$pubDt='20140715T035959Z';
$pubDt=str_replace(array('T','Z'),array('',''),$pubDt);   
$format = 'Ymdhis';
$date = DateTime::createFromFormat($format, $pubDt);
echo $newPubdate = $date->format('Y-m-d H:i:s');  //"prints" 2014-07-15 03:59:59

你把代码搞错了。我要从PHP到iCal,而不是从iCal到PHP。:)
$temp->format('Ymd\TGis\Z')有什么问题?你做得对。我想你应该改变你的
echo$eventStart
返回$eventStart