原创

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

温馨提示:
本文最后更新于 2022年07月23日,已超过 809 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我
<?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;
    }

}

仙士可博客

正文到此结束
本文目录