php计算两个时间段内的 工作日 工作小时
<?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);
php计算两个时间段内的 工作日 工作小时的更多相关文章
- PHP计算两个时间段是否有交集(边界重叠不算)
优化前的版本: /** * PHP计算两个时间段是否有交集(边界重叠不算) * * @param string $beginTime1 开始时间1 * @param string $endTime1 ...
- 用VBA计算两个日期之间的工作日(去掉周末两天)
最近公司HR和Finance想算员工的工作天数,想让我帮忙写些VBA,自己从网上找了下代码,自己再改改,以下来自网络. 计算两个日期之间的工作日,用VBA,因量大,最好用数组做 Sub kk() Di ...
- C 语言实例 - 计算两个时间段的差值
C 语言实例 - 计算两个时间段的差值 C 语言实例 C 语言实例 计算两个时间段的差值. 实例 #include <stdio.h> struct TIME { int seconds; ...
- Oracle 查询两个时间段内的所有日期列表
1.查询某时间段内日期列表 select level,to_char(to_date('2013-12-31','yyyy-mm-dd')+level-1,'yyyy-mm-dd') as date_ ...
- sql server两个时间段内,求出周末的量
公司有个表记录了出差(加班)的初始时间和截止时间,现在要计算出加班时间,之前的设计并没有考虑到这部分,因此本人通过sql重新计算周末数 表formmain starttime endtime 使用游标 ...
- mysql 计算两个日期之间的工作日天数
创建透视表t500 建表 CREATE TABLE `t500` ( `id` int(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`) ) ENGINE ...
- SQL计算两个时间段相隔时间
SQL语句: select cast(floor(datediff(minute,时间1,时间2) / 1440) as varchar)+'天'+ cast(floor((datediff(minu ...
- java计算两个时间相差(天、小时、分钟、秒)
public static Long dateDiff(String startTime, String endTime, String format, String str) { // 按照传入的格 ...
- 计算一段日期内的周末天数的php代码(星期六,星期日总和)
代码如下: /*| Author: Yang Yu <niceses@gmail.com>| @param char|int $start_date 一个有效的日期格式,例如:200910 ...
随机推荐
- 设计并测试Trapezium类 代码参考
#include <iostream> using namespace std; class Trapezium { private: int x1,y1,x2,y2,x3,y3,x4,y ...
- Java实现 LeetCode 392 判断子序列
392. 判断子序列 给定字符串 s 和 t ,判断 s 是否为 t 的子序列. 你可以认为 s 和 t 中仅包含英文小写字母.字符串 t 可能会很长(长度 ~= 500,000),而 s 是个短字符 ...
- Java实现 LeetCode 345 反转字符串中的元音字母
345. 反转字符串中的元音字母 编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "holle" 示例 ...
- Java实现 LeetCode 287 寻找重复数
287. 寻找重复数 给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数.假设只有一个重复的整数,找出这个重复的数. 示例 ...
- Elasticsearch 别管原理,先run起来
少点代码,多点头发 本文已经收录至我的GitHub,欢迎大家踊跃star 和 issues. https://github.com/midou-tech/articles 看文章有两点需要注意: 本公 ...
- 简单5步,轻松debug K8S服务!
作者: Ram Rai,性能.可扩展性以及软件架构的爱好者 原文链接: https://medium.com/better-programming/debug-your-kubernetes-serv ...
- 红楼梦 + 写入 MySQL
import requests import re import pymysql from bs4 import BeautifulSoup conn = pymysql.Connect(host=' ...
- CMDB 和自动化运维
目录 传统运维和自动化运维的对比 CMDB CMDB 的几种实现方式 传统运维和自动化运维的对比 1.企业中,项目的发布流程 产品经理调研需求 -->三方开会讨论(开发,产品,运维,测试) -– ...
- mysql 双机互备份
//1.创建用户CREATE USER 'dump'@'%' IDENTIFIED BY 'dump'; //2.开放权限GRANT ALL ON *.* TO 'dump'@'%'; //3.刷新权 ...
- ubuntu12.04 qtcreate支持中文输入
1.sudo apt-get install ibus-qt4 2.重启电脑 reboot