dft99
dft99

Reputation: 69

PHP DateTime Interval errors

if (empty($krow['BUSSTRT'])){ 
    $busts = $ts->add(new DateInterval('PT6i5s'));
    $busDate = $busts->format('m/d/Y H:i:s');
    echo "busDate:".$busDate."<br>\n";
}else{
    $busts = new DateTime($krow['BUSSTRT']);
    $busDate = $busts->format('m/d/Y H:i:s');
    echo "busSTRT:".$busDate."<br>\n";
}   

if (empty($krow['LAMISTRT'])){ 
    echo "lamistrt is empty::::";
    $lamts = $busts->add(new DateInterval('PT11is'));
    $lamDate = $lamts->format('m/d/Y H:i:s');
    echo "lamDate:".$lamDate."<br>\n";
}else{
    $lamts = new DateTime($krow['LAMISTRT']);
    $lamDate = $lamts->format('m/d/Y H:i:s');
    echo "lamistrt:".$lamDate."<br>\n";
} 

The code above is throwing the following error:

PHP Fatal error: Uncaught exception 'Exception' with message 'DateInterval::__construct(): Unknown or bad format (PT11i3s)'

when $bustDate is:

busSTRT:02/06/2015 03:53:56 lamistrt is empty::::

What am I missing here?

Upvotes: 1

Views: 1523

Answers (1)

John Conde
John Conde

Reputation: 219804

When using DateInterval() to create an interval you use M for minutes, not i. Also, if there are no seconds you must omit it from the interval declaration:

$busts = $ts->add(new DateInterval('PT6M5S'));
$lamts = $busts->add(new DateInterval('PT11M'));

i is used for getting the number of minutes in a date interval:

echo $intervalObj->i; // get minutes

Upvotes: 1

Related Questions