1

我正在尝试从 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 =时间&格式=xml&limit=8&nodata=404

Github 上的对应问题:https ://github.com/felakuti4life/Seismokraft/issues/1

4

2 回答 2

3

我认为它实际上比你想象的要简单。以下应该有效:

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

看到它在行动

于 2013-10-07T19:47:35.560 回答
0
$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");
于 2013-10-07T19:53:46.370 回答