-1

I am building a SLA KPI. The SLA is based on ticket type and priority, each of these have a due date.

The thing is, I only have the field created_at, I don't have this due date, so I need to calculate it, and as mentioned above, use the params priority and type to define the right date.

But my real problem is, how to calculate it on PHP, considering operation days and hours, monday to friday, 8:00 to 18:00 For example.

The ticket was created at 2019-06-04 08:00:00 and its deadline is 16 hours. So it would be, 2019-06-06 08:00:00

Jean Lima
  • 93
  • 9
  • 3
    https://www.php.net/manual/en/datetime.examples-arithmetic.php, https://www.php.net/manual/en/ref.datetime.php – David Jun 10 '19 at 17:43
  • You are right, I've forgotten to mention the lunch time – Jean Lima Jun 10 '19 at 17:49
  • Thanks, I will read it – Jean Lima Jun 10 '19 at 17:57
  • Possible duplicate of [Adding Days to a Date with PHP](https://stackoverflow.com/questions/17959959/adding-days-to-a-date-with-php) – Dave Jun 10 '19 at 17:58
  • No, it isn't, i've read that post, but i need to calculate the hours too, and I don't know how to do this. I came here to ask for help at least to know where to look for the right path. Thanks – Jean Lima Jun 10 '19 at 18:00

1 Answers1

0

After digging a lot looking for a answer, I've found a function that solved my problem.

    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;
}

Thank you all.

Jean Lima
  • 93
  • 9