计算在工作日时间推迟时间的算法

举报
仙士可 发表于 2023/06/30 12:18:09 2023/06/30
【摘要】 <?phpnamespace App\Http\Services;/** * 工作日时间类 */class WorkTimeService{ protected $workTimeRange = [ "9:00:00" => "12:30:00", "13:30:00" => "18:00:00" ]; protected $holidayDa...
<?php

namespace App\Http\Services;
/**
 * 工作日时间类
 */
class WorkTimeService
{
    protected $workTimeRange = [
        "9:00:00"  => "12:30:00",
        "13:30:00" => "18:00:00"
    ];
    protected $holidayDay = [
        "2022-01-01" => "2022-01-03",
        "2022-01-31" => "2022-02-06",
        "2022-04-03" => "2022-04-05",
        "2022-04-30" => "2022-05-04",
        "2022-09-10" => "2022-09-12",
        "2022-10-01" => "2022-10-07",
    ];

    protected $holidayExtraWorkDay = [
        "2022-01-29" => "2022-01-30",
        "2022-04-02" => "2022-04-02",
        "2022-04-24" => "2022-04-24",
        "2022-05-07" => "2022-05-07",
        "2022-10-08" => "2022-10-09",
    ];

    protected $workDayData = [
        "11111111111111" => "222222222222"//时间戳
    ];
    protected $holidayData = [
        "11111111111111" => "222222222222"//时间戳
    ];


    public function __construct($workTimeRange = [], $holidayDay = [], $holidayExtraWorkDay = [])
    {
        ini_set('date.timezone', 'Asia/Shanghai');

        if (!empty($workTimeRange)) {
            $this->workTimeRange = $workTimeRange;
        }
        if (!empty($holidayDay)) {
            $this->holidayDay = $holidayDay;
        }
        if (!empty($holidayExtraWorkDay)) {
            $this->holidayExtraWorkDay = $holidayExtraWorkDay;
        }

        $this->initDay();
    }

    public function initDay()
    {
        foreach ($this->holidayDay as $startDate => $endDate) {
            $this->holidayData[strtotime($startDate)] = strtotime($endDate);
        }
        ksort($this->holidayData);
        foreach ($this->holidayExtraWorkDay as $startDate => $endDate) {
            $this->workDayData [strtotime($startDate)] = strtotime($endDate);
        }
        ksort($this->workDayData);
    }

    public function getDelayWorkTime($datetime, $delayTime = 0)
    {
        //判断当日是否为上班日
        $isWorkDay = $this->checkIsWorkDay($datetime);
        $time = strtotime($datetime);
        if ($isWorkDay) {
            //如果是,则计算上班时间
            $workTimeDate = $this->initWorkTime(date("Y-m-d", strtotime($datetime)));
            foreach ($workTimeDate as $startTime => $endTime) {
                //比如当天12点半下班,1点半上班,你现在时间为12点半,则可以继续推进到1点半的时间段
                if ($time < $startTime) {
                    $time = $startTime;
                }
                if ($time >= $startTime && $time <= $endTime) {//如果当前工作时间在这个时间段内
                    $decTime = $endTime - $time;//算出离下班还有多少时间
                    if ($decTime > $delayTime) {//如果延时时间小于下班时间,则说明完成循环
                        $decTime = $delayTime;
                    }
                    $time = $time + $decTime;//时间往前推进
                    $delayTime = $delayTime - $decTime;//延时时间减少
                }
                if ($delayTime<=0){
                    break;
                }
            }
            if ($delayTime <= 0) {
                return date("Y-m-d H:i:s", $time);
            } else {
                $datetime = date("Y-m-d", strtotime($datetime) + 86400);
                return $this->getDelayWorkTime($datetime, $delayTime);
            }
        } else {
            //如果不是上班日,则直接推进时间
            $datetime = date("Y-m-d", strtotime($datetime) + 86400);
            return $this->getDelayWorkTime($datetime, $delayTime);
        }
    }

    public function checkIsWorkDay($date)
    {
        $week = date("w", strtotime($date));
        if (in_array($week, [1, 2, 3, 4, 5])) {//周一到周五
            //验证是否为节假日,如果是则不用上班
            if ($this->checkHoliday($date)) {
                return false;
            }
            return true;
        } else {//周末
            if ($this->checkWorkDay($date)) {//验证是否要调休,如果是则需要上班
                return true;
            }
            return false;
        }
    }

    public function checkHoliday($date)
    {
        $time = strtotime($date);
        foreach ($this->holidayData as $startTime => $endTime) {
            if ($time >= $startTime && $time <= $endTime) {
                return true;
            }
        }
        return false;
    }

    public function checkWorkDay($date)
    {
        $time = strtotime($date);
        foreach ($this->workDayData as $startTime => $endTime) {
            if ($time >= $startTime && $time <= $endTime) {
                return true;
            }
        }
        return false;
    }

    public function initWorkTime($date)
    {
        $workTimeData = [];
        foreach ($this->workTimeRange as $startTime => $endTime) {
            $workTimeData[strtotime($date . " " . $startTime)] = strtotime($date . " " . $endTime);
        }
        ksort($workTimeData);
        return $workTimeData;
    }

}
 
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。