从票证创建日期php

时间:2019-06-10 17:41:28

标签: php deadlines

我正在构建SLA KPI。 SLA基于票证类型和优先级,每个都有到期日期。

问题是,我只有一个字段created_at,没有这个到期日,因此我需要进行计算,如上所述,请使用params优先级和类型来定义正确的日期。

但是我真正的问题是,如何在PHP上计算它,考虑到工作日和工作时间,周一至周五,8:00至18:00 例如。

故障单是在2019-06-04 08:00:00创建的,截止日期为16小时。 就是这样,2019-06-06 08:00:00

1 个答案:

答案 0 :(得分:0)

经过大量的寻找答案之后,我发现了一个可以解决我的问题的函数。

    function addRollover($givenDate, $addtime, $dayStart, $dayEnd, $weekDaysOnly) {

    //Break the working day start and end times into hours, minuets
    $dayStart = explode(',', $dayStart);
    $dayEnd = explode(',', $dayEnd);

    //Create required datetime objects and hours interval
    $datetime = new DateTime($givenDate);

    $endofday = clone $datetime;
    $endofday->setTime($dayEnd[0], $dayEnd[1]); //set end of working day time


    $interval = 'PT'.$addtime.'H';
    //Add hours onto initial given date
    $datetime->add(new DateInterval($interval));

    //if initial date + hours is after the end of working day

    if($datetime > $endofday)
    {
        //get the difference between the initial date + interval and the end of working day in seconds
        $seconds = $datetime->getTimestamp()- $endofday->getTimestamp();

        //Loop to next day
        while(true)
        {
            $endofday->add(new DateInterval('PT24H'));//Loop to next day by adding 24hrs
            $nextDay = $endofday->setTime($dayStart[0], $dayStart[1]);//Set day to working day start time
            //If the next day is on a weekend and the week day only param is true continue to add days
            if(in_array($nextDay->format('l'), array('Sunday','Saturday')) && $weekDaysOnly)
            {
                continue;
            }
            else //If not a weekend
            {
                $tmpDate = clone $nextDay;
                $tmpDate->setTime($dayEnd[0], $dayEnd[1]);//clone the next day and set time to working day end time
                $nextDay->add(new DateInterval('PT'.$seconds.'S')); //add the seconds onto the next day
                //if the next day time is later than the end of the working day continue loop
                if($nextDay > $tmpDate)
                {
                    $seconds = $nextDay->getTimestamp()-$tmpDate->getTimestamp();
                    $endofday = clone $tmpDate;
                    $endofday->setTime($dayStart[0], $dayStart[1]);

                }
                else //else return the new date.
                {
                    return $endofday;


                }
            }
        }
    }

    return $datetime;
}

谢谢大家。