如何在PHP中添加时间值?

时间:2014-01-28 08:14:29

标签: php yii

我试图做start_time + appt_duration = end_time; start_time是e:g:08:00:00 持续时间e:g:01:30:00

但是结束时间是:00:54:08

所有属性都在数据库中配置为“time”。

代码:

     $time= $model->start_time;

     $duration=$model->appt_duration;

     $duration_array = explode(':', $duration);

     $length = ((int)$duration_array[0] * 3600) + ((int)$duration_array[1] * 60) + (int)$duration_array[2];
     $target = $length + $time;

     $model->end_time= $target;

2 个答案:

答案 0 :(得分:4)

我优先使用来自http://www.php.net/manual/en/datetime.add.php\DateTime对象:

$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P10D'));
echo $date->format('Y-m-d'), PHP_EOL;

另请参阅\DateTime::createFromFormat() - http://www.php.net/manual/de/datetime.createfromformat.php

答案 1 :(得分:1)

试试这个:

    $length = ((int)$duration_array[0] * 3600) + ((int)$duration_array[1] * 60)
    + (int)$duration_array[2];
    $model->end_time = date("H:i:s",  
            strtotime("+".$length." seconds",strtotime($model->start_time))
            );