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 ...
随机推荐
- SpringBoot的 HelloWorld
SpringBoot HelloWorld 功能需求 浏览器发送hello请求,服务器接收请求并处理,相应HelloWorld字符串 1.创建一个maven工程:(jar) 2.导入SpringB ...
- 对于使用progisp软件进行ISP编程时进入不了编程模式的解决方法
标题: 对于使用progisp软件进行ISP编程时无法进入编程模式的解决方法 作者: 梦幻之心星 347369787@QQ.com 标签: [progisp, 软件] 目录: 软件 日期: 2019- ...
- 拨开云雾-Verilog是个大杂烩(中性)
https://mp.weixin.qq.com/s/HKxX_79DtnXmFU1Mwt1GwA 一. 有意为之 Verilog是个大杂烩,这是有意而为之. Verilog IEEE S ...
- Attribute (XXX) is obsolete. Its use is discouraged in HTML5 documents.
这种警告主要是因为这些属性在HTML5中过时了,并不影响代码运行,但是一些强迫症就会非常难受. 解决办法: 将程序的顶部的这句: !DOCTYPE 修改为: !DOCTYPE html PUBLIC ...
- Java实现 蓝桥杯 算法训练 Lift and Throw
试题 算法训练 Lift and Throw 问题描述 给定一条标有整点(1, 2, 3, -)的射线. 定义两个点之间的距离为其下标之差的绝对值. Laharl, Etna, Flonne一开始在这 ...
- Java实现 LeetCode 377 组合总和 Ⅳ
377. 组合总和 Ⅳ 给定一个由正整数组成且不存在重复数字的数组,找出和为给定目标正整数的组合的个数. 示例: nums = [1, 2, 3] target = 4 所有可能的组合为: (1, 1 ...
- (十)DVWA之SQL Injection--测试分析(Impossible)
DVWA之SQL Injection--测试分析(Impossible) 防御级别为Impossible的后端代码:impossible.php <?php if( isset( $_GET[ ...
- Mac卸载.net core sdk
NET Core cli提供了卸载脚本 https://github.com/dotnet/cli/tree/master/scripts/obtain/uninstall dotnet-uninst ...
- C和C++中static的比较
using namespace std; class A{ private: static int a;//由static修饰的变量仅仅是一个声明,不能在此处进行初始化,需要在类的外部初始化. voi ...
- Python 读取和输出到txt
读txt文件 python常用的读取文件函数有三种read().readline().readlines() read() #一次性读取文本中全部的内容,以字符串的形式返回结果 with open(& ...