根据两个给定时间计算时间长度

时间:2017-09-21 20:32:21

标签: php time php-7 calculation

需要帮助来计算FactoryGirl.define do factory :post do title 'This is a title' body 'This is a body' published_at Time.now association :user end end $work_in_daytime

$work_in_nighttime

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

如果开始结束跨越夜间开始或夜间结束,则需要将工作时间分成单独的项目。我建议转换为datetime对象并使用dateinterval使其更容易获得持续时间。

答案 1 :(得分:0)

  <?php
    $start_time = strtotime("2017-09-01 10:42:00");
    $end_time = strtotime("2017-09-02 10:21:00");
    //Divide seconds by 60 to get minutes or whatever is appropriate and round off
    echo $difference = round(abs($to_time - $from_time) / 60,2). " minutes";
?>