我已經看過關於同一個錯誤的其他問題,但我無法將它們應用於我的情況。PHP日期時間:解析時間字符串失敗
這是我得到的錯誤:
Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct() [datetime.--construct]: Failed to parse time string (2013-07-22164:50:00) at position 10 (1): Unexpected character' in /Applications/XAMPP/xamppfiles/htdocs/Festival_Planner/index.php:88 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/Festival_Planner/index.php(88): DateTime->__construct('2013-07-22164:5...', Object(DateTimeZone)) #1 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/Festival_Planner/index.php on line 88
這是與正在生成該錯誤的行循環:
for($iCount2=0;$iCount2<count($ascreenings);$iCount2++){
$ocurrentscreening = $ascreenings[$iCount2];
///////// THIS IS LINE 88:
$time = new DateTime($ocurrentscreening->date.''.$ocurrentscreening->starttime,new DateTimeZone('Pacific/Auckland'));
$displayTime = date_format($time, 'g:ia');
$sLabel = $ocurrentscreening->date.', '.$displayTime.'.';
$oForm->makeCheckBox("screening".$ocurrentscreening->screeningid, $sLabel, $ocurrentscreening->screeningid);
}
這是一個類似的循環,它的工作,使用與我在88行上完全相同的代碼結構。
for($iCount=0;$iCount<count($aUsersScreenings);$iCount++){
$odisplayedscreening = $aUsersScreenings[$iCount];
$ofilm = new film();
$ofilm->load($odisplayedscreening->filmid);
$title = $ofilm->title;
$time = new DateTime($odisplayedscreening->date.''.$odisplayedscreening->starttime,new DateTimeZone('Pacific/Auckland'));
$displayTime = date_format($time, 'g:ia');
$sHTML .= '
<div class="selected" id="screening'.$odisplayedscreening->screeningid.'">
<span>'.$title.'</span>.'.$displayTime.'.
</div>
';
}
它清楚地告訴你一切..在位置10(1)解析時間字符串(2013-07-22164:50:00):意外字符。看看這個字符串,年份和時間都粘在一起,顯然是畸形的。這不是代碼,而是輸入。 DateTime類另一個名爲'createFromFormat'的方法,您可以在其中指定輸入格式,無論它看起來多麼奇怪。另外,如果你有'$ time-> format('g:ia')'可用,爲什麼要使用DateTime對象和'date_format'函數呢? –
明白了,我錯誤地在我的數據庫中輸入了一個時間字符串。這很尷尬......謝謝你! – user2586422