<?php

class WorkTime
{
// 定义工作日 [1, 2, 3, 4, 5, 6, 0]
public $week_workingday = [1, 2, 3, 4, 5]; // 定义上下班时间
public $on_duty_time = '9:00:00';
public $off_duty_time = '18:00:00'; //每天工作时长
public $oneday_hours = null; public function __construct()
{
if (empty($this->oneday_hours)) {
$this->oneday_hours = $this->off_duty_time - $this->on_duty_time;
}
} public function get_working_hours(int $start_time, int $over_time)
{
// 如果工作日列表为空 返回0
$workingdays = $this->workingdays($start_time, $over_time); if (empty($workingdays)) {
return 0;
} // 如果开始时间不是工作日,将开始时间调整为第一个工作日的上班时间
// 如果截止时间不是工作日,将开始时间调整为最后一个工作日的下班时间
if (!in_array(date('Y/m/d', $start_time), $workingdays)) {
$start_time = strtotime($workingdays[0] . ' ' . $this->on_duty_time);
} if (!in_array(date('Y/m/d', $over_time), $workingdays)) {
$over_time = strtotime(end($workingdays) . ' ' . $this->off_duty_time);
} // 如果开始时间与截止时间是同一天,直接计算
// 反之分别计算开始时间与截止时间
if (date('Y/m/d', $start_time) == date('Y/m/d', $over_time)) {
$sec = $over_time - $start_time;
if ($sec > 3600 * $this->oneday_hours) {
$sec = 3600 * $this->oneday_hours;
}
// 昨天到了计算秒数
} else {
// 计算开始日工作时间
$start_day_sec = strtotime($workingdays[0] . ' ' . $this->off_duty_time) - $start_time;
if ($start_day_sec > 3600 * $this->oneday_hours) {
$start_day_sec = 3600 * $this->oneday_hours;
}
// 计算截止日工作时间
$over_day_sec = $over_time - strtotime(end($workingdays) . ' ' . $this->on_duty_time);
if ($over_day_sec > 3600 * $this->oneday_hours) {
$over_day_sec = 3600 * $this->oneday_hours;
} $all_day_sec = ((count($workingdays) - 2) * $this->oneday_hours) * 3600;
$sec = $start_day_sec + $over_day_sec + $all_day_sec;
} return $sec / 3600;
} # 计算工作日(包含开始与截止日期)
protected function workingdays($start_time, $over_time)
{
$start_time = strtotime('-1 day', $start_time);
$over_time = strtotime('-1 day', $over_time); $new_workingdays = $this->new_workingdays();
$new_holidays = $this->new_holidays();
$workingdays = []; while ($start_time < $over_time) {
$start_time = strtotime('+1 day', $start_time);
$is_holidays = in_array(date('w', $start_time), $this->week_workingday) && !in_array(date('Y/m/d', $start_time), $new_holidays);
$is_workingdays = in_array(date('Y/m/d', $start_time), $new_workingdays); if ($is_holidays || $is_workingdays) {
$workingdays[] = date('Y/m/d', $start_time);
}
} return $workingdays;
} # 新增工作日
protected function new_workingdays()
{
$days = [
'2020/05/09',
]; return $days;
} # 新增休息日
protected function new_holidays()
{
$days = [
'2020/05/01',
'2020/05/04',
'2020/05/05',
]; return $days;
} }
$start_time = strtotime('2020-05-06 10:00:00');
$over_time = strtotime('2020-05-11 18:00:00'); $work = new WorkTime();
$working_hours = $work->get_working_hours($start_time, $over_time); var_dump($working_hours);

最新文章

  1. div宽高设置为百分比
  2. jeasyUI的treegrid批量删除多行(转载)
  3. ajax跨域之设置Access-Control-Allow-Origin
  4. 【Alpha版本】 第六天 11.14
  5. cocos2dx 3.0 之 lua 创建类 (二)
  6. MySQL基础之第18章 性能优化
  7. STL map 用法
  8. 在ASP.NET 中调用RSACryptoServiceProvider失败,提示未找到文件
  9. Dll方式的线程,需要引用这个
  10. Linux下使用虚拟网卡的ingress流控(入口流控)
  11. c++ 10
  12. displayport-2
  13. 在线制作css动画——cssanimate
  14. [bzoj4821][Sdoi2017]相关分析
  15. Spark2.X管理与开发
  16. 17秋 软件工程 团队第五次作业 Alpha
  17. TP5一对一、一对多关联模型的使用
  18. ES3之cookie
  19. 初识thinkphp(5)
  20. Hibernate(八)

热门文章

  1. burpsuite 2.0beta体验
  2. 关于MySQL无法启动的一点问题
  3. background-color的覆盖范围
  4. Java实现蓝桥杯历届试题买不到的数目
  5. Java实现 LeetCode 474 一和零
  6. Java实现 蓝桥杯VIP 算法训练 ALGO-85进制转换
  7. Java实现 LeetCode 233 数字 1 的个数
  8. Java实现 蓝桥杯 历届试题 地宫取宝
  9. Java实现第九届蓝桥杯等腰三角形
  10. Java实现第十届蓝桥杯数列求值