【问题标题】:Calculate time length based on two given times根据两个给定时间计算时间长度
【发布时间】:2017-09-21 20:32:20
【问题描述】:

需要帮助计算$work_in_daytime$work_in_nighttime

$nightStart = '22:00';
$nightEnd = '07:00';

$workers = array(
    '0' => array(
        'name'  => 'Lyons',
        'start' => '15:15',
        'end'   => '23:45'
    ),

    '1' => array(
        'name'  => 'Santos',
        'start' => '10:00',
        'end'   => '22:00'
    ),

    '2' => array(
        'name'  => 'Montgomery',
        'start' => '22:30',
        'end'   => '08:00'
    )
); 


foreach ($workers as $worker) {
    $length_of_work = abs(strtotime($worker['start']) - strtotime($worker['end'])) / 3600;
    $work_in_daytime = '';
    $work_in_nighttime = '';
}

感谢您的帮助。

【问题讨论】:

  • 如果有人开始在21:00 工作会怎样。在这种情况下,您的 $nightStart = '22:00' 将无济于事
  • @RomanPerekhrest 如果有人在 21:00 开始工作,则会在白天 ($work_in_daytime) 中增加一小时。

标签: php time php-7 calculation


【解决方案1】:

如果从开始到结束跨越夜间开始或夜间结束,您需要将工作时间分成单独的项目。我建议转换为 datetime 对象并使用 dateinterval 来轻松获取持续时间。

【讨论】:

  • 你能举个例子吗?
  • 没有。很乐意为您提供帮助,但很遗憾无法为您提供帮助。
【解决方案2】:
  <?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";
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-26
    • 2014-09-20
    • 1970-01-01
    • 1970-01-01
    • 2014-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多