我正在尝试从XML文件中解析日期,并以与原始日期相同的格式以字符串形式返回日期,但8小时前除外。
原始日期采用以下格式:
'yyyy-mm-ddThh:mm:ss.ffff'
因此日期始终是固定长度。
示例:"2013-10-06T14:00:40.1000"
在这种情况下,使用 date_parse() 和 date_modify() 函数的合适方法是什么?
当前代码:
public function setTimeSeriesStartDate(){
//FIXME
//replace T with space to make it parsable by date_parse()
$tempDate = $this->date;
$tempDate[10] = ' ';
$parsedDate = new DateTime(date_parse($tempDate));
$parsedDate->modify('-'.$this->daysBeforeEvent.' day');
$farmattedDate=$parsedDate->format('Y-m-d H:i:s');
if($formattedDate){
$this->timeSeriesStartDate= $formattedDate;
$this->timeSeriesStartDate[10]='T';
}
else {$this->timeSeriesStartDate = $this->date;}
}
日期来自的 XML 文件:http://service.iris.edu/fdsnws/event/1/query?starttime=2010-02-27T06:30:00&endtime=2013-10-07&minmag=2.0&maxmag=4.5&includeallorigins=true&orderby=time&format=xml&limit=8&nodata=404
Github上的相应问题:https://github.com/felakuti4life/Seismokraft/issues/1
我认为它实际上比你想象的要简单。以下方法应该有效:
//$tempDate = $this->date; <-- REMOVE
//$tempDate[10] = ' '; <-- REMOVE
$parsedDate = new DateTime($tempDate);
$parsedDate->modify('-8 hours');
或
//$tempDate = $this->date; <-- REMOVE
//$tempDate[10] = ' '; <-- REMOVE
$parsedDate = new DateTime($tempDate);
$parsedDate->sub(new DateInterval('PT8H'));
查看实际效果
$tempDate = $this->date;
$tempDate = date_add($tempDate,date_interval_create_from_date_string("-8 hours"));
$tempDate = date_format($tempDate,"Y/m/d H:i:s");