2013-10-07 41 views
1

我試圖解析XML文件中的日期,並以與原始日期相同的格式返回日期字符串,除了早8小時在PHP中解析和修改日期

原始日期是以下格式:
'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

+0

日期時間:: createFromFormat();也許? – bwoebi

回答

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

See it in action

+2

是的DateTime和DateInterval類必須學習什麼時候處理PHP日期> –

+0

這很好,謝謝。但它會用時區信息(+02:00)替換分數以符合ISO8601格式。我應該用substr_replace()更改字符串輸出還是嘗試修改日期格式? – Kadinski

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"); 
+0

John比我鍵入的速度更快,可能有更高效的答案。 –