2012-04-13 127 views
3

幫助解決這個錯誤致命錯誤:未捕獲的異常「異常」有消息「日期時間::

Fatal error: Uncaught exception 'Exception' with message 'DateTime::_construct() [datetime.--construct]: Failed to parse time string (--) at position 0 (-): Unexpected character' in Z:\home\plati\www\view.php:110 Stack trace: #0 Z:\home\plati\www\view.php(110): DateTime->_construct('--') #1 {main} thrown in Z:\home\plati\www\view.php on line 110

$newday = $a['dayz']; 
$endmonth = $a['monthz']; 
$newyear = $a['yearz']; 
$date = new DateTime("$newyear-$endmonth-$newday"); 
$date->modify('+8 day'); 
$year = $date->format('Y'); 
$month = $date->format('m'); 
$day = $date->format('d'); 

回答

1

help fix this error

您可以例外通過捕捉它輕鬆應對,這將解決在這個意義上的錯誤,你不必理會該錯誤不再:

try { 
    $newday = $a['dayz']; 
    $endmonth = $a['monthz']; 
    $newyear = $a['yearz']; 
    $date = new DateTime("$newyear-$endmonth-$newday"); 
    $date->modify('+8 day'); 
    $year = $date->format('Y'); 
    $month = $date->format('m'); 
    $day = $date->format('d'); 
} catch(Exception $e) { 
    # do nothing 
} 

至少在有一點你需要做錯誤處理。例外情況要求您這樣做,DateTime會引發異常。

4

你有沒有試過打印值"$newyear-$endmonth-$newday"?因爲從錯誤,它看起來像變量沒有填充任何內容。所以請發佈該字符串的結果。

1

$ a中的值爲空;檢查你的輸入。

+0

oh yes $ a is empty tyvm – 2012-04-13 12:45:09

相關問題