php – 计算工作班次
发布时间:2020-12-13 17:07:25 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试根据工作小时数和工作时间来计算某人的工作时间. 例如: 轮班模式为07:00至15:00为“早晨”,15:00至23:00为“下午”,23:00至07:00为“晚上”. 因此,如果我从08:00开始并在17:30结束,这意味着我将在早上7点和下午2点开始付款. 解决方法 我从一段
我正在尝试根据工作小时数和工作时间来计算某人的工作时间.
例如: 轮班模式为07:00至15:00为“早晨”,15:00至23:00为“下午”,23:00至07:00为“晚上”. 因此,如果我从08:00开始并在17:30结束,这意味着我将在早上7点和下午2点开始付款. 解决方法
我从一段时间前开发的项目中修改了一段代码.
函数“intersection”计算两个范围s1-e1和s2-e2之间重叠的时间量. 请注意,所有时间都以秒为单位,我们在第二天的时间内添加3600 * 24秒. <?php function intersection($s1,$e1,$s2,$e2) { if ($e1 < $s2) return 0; if ($s1 > $e2) return 0; if ($s1 < $s2) $s1 = $s2; if ($e1 > $e2) $e1 = $e2; return $e1 - $s1; } $start = strtotime("07:00"); $end = strtotime("17:30"); // $end = strtotime("05:30") + 3600*24; // the work ended at 05:30 morning of the next day $morning_start = strtotime("07:00"); $morning_end = strtotime("15:00"); $afternoon_start = strtotime("15:00"); $afternoon_end = strtotime("23:00"); $night_start = strtotime("23:00"); $night_end = strtotime("07:00") + 3600*24; // 07:00 of next day,add 3600*24 seconds echo "morning: " . intersection( $start,$end,$morning_start,$morning_end ) / 3600 . " hoursn"; echo "afternoon: " . intersection( $start,$afternoon_start,$afternoon_end ) / 3600 . " hoursn"; echo "night: " . intersection( $start,$night_start,$night_end ) / 3600 . " hoursn"; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |