PHP DateTime:无法解析时间字符串

时间:2013-07-16 09:40:49

标签: php string parsing datetime

我已经查看了有关同一错误的其他问题,但我无法将它们应用到我的情况中。

这是我得到的错误:

  

致命错误:带有消息的未捕获异常'异常'   'DateTime :: __ construct()[datetime .-- construct]:无法解析   时间字符串(2013-07-22164:50:00)位置10(1):意外   人物'in   /Applications/XAMPP/xamppfiles/htdocs/Festival_Planner/index.php:88   堆栈跟踪:#0   /Applications/XAMPP/xamppfiles/htdocs/Festival_Planner/index.php(88):   DateTime-> __ construct('2013-07-22164:5 ...',Object(DateTimeZone))#1   抛出了{main}   /Applications/XAMPP/xamppfiles/htdocs/Festival_Planner/index.php on   第88行

这是带有生成错误的行的for循环:

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

这是一个类似的for循环,可以使用与第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>
        ';
        }

1 个答案:

答案 0 :(得分:1)

您需要在日期和时间之间留出空格

$time = new DateTime($ocurrentscreening->date.' '.$ocurrentscreening->starttime,new DateTimeZone('Pacific/Auckland'));