致命错误:未捕获的异常 'Exception',消息为 'DateTime ::

3

请帮助解决这个错误

致命错误: 异常 'Exception',信息为 'DateTime::_construct() [datetime.--construct]: Failed to parse time string (--) at position 0 (-): Unexpected character',位于 Z:\home\plati\www\view.php 的第 110 行。堆栈跟踪: #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');
3个回答

4

你是否曾尝试打印值为"$newyear-$endmonth-$newday"的变量?因为从错误信息来看,这些变量似乎没有填充任何内容。请输出该字符串的结果。


1

$a 中的值为空,请检查您的输入。


1

帮忙修复这个错误

你可以通过捕获异常来轻松处理它,这将修复该错误,使你不再需要担心它:

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会抛出异常。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接