Home > database >  Get minutes from two timestamps, which are on a Sunday in PHP
Get minutes from two timestamps, which are on a Sunday in PHP

Time:02-05

I have two timestamps, which possibly can be any date and time. I want to get all minutes, which were on Sunday. For a better understanding: The start and and end timestamp represent a date and time where an employee starts his work and finish his work. I want to get the minutes in sum, which the employee worked on a Sunday.

Here is my code:

function get_sunday_hours_from_timestamps($startTimestamp, $endTimestamp) {
    $start = new DateTime();
    $start->setTimestamp($startTimestamp);
    $end = new DateTime();
    $end->setTimestamp($endTimestamp);
    $workedMinutes = 0;
    $current = clone $start;
    while ($current <= $end) {
        $next = clone $current;
        $next->modify('next day');
        if ($current->format('w') == 0) {
            $dayStart = ($current < $start) ? $start : $current;
            $dayEnd = ($next > $end) ? $end : $next;
            $diff = $dayEnd->diff($dayStart);
            $minutes = $diff->days * 1440   $diff->h * 60   $diff->i;
            $workedMinutes  = $minutes;
        }
        $current = $next;
    }

    return $workedMinutes / 60;
    // return $workedMinutes;
}

CodePudding user response:

Thank you for your input. I was able to solve the problem. Hope this helps anybody else.

 function get_sunday_hours_from_timestamps($startTimestamp, $endTimestamp) {
    $totalMinutes = 0;


    $startDay = strtotime("midnight", $startTimestamp);
    $endDay = strtotime("tomorrow", $endTimestamp) - 1;


    for ($currentDay = $startDay; $currentDay <= $endDay; $currentDay = strtotime(" 1 day", $currentDay)) {
        if (date("l", $currentDay) == "Sunday") {

        $start = max($startTimestamp, $currentDay);
        $end = min($endTimestamp, strtotime("tomorrow", $currentDay) - 1);


        $totalMinutes  = ($end - $start) / 60;
        }
    }


    return round($totalMinutes / 15) * 0.25;
}

CodePudding user response:

Warning: The solution below is highly inefficient and extremely slow, especially for large time periods as input. It only serves to illustrate a naive approach in an easily readable form. You can use this as a starting point, but use it wisely!


A very naive approach to your problem (count sunday minutes in a given time period) could be: Iterate over every minute in you period, check if that minute is on a sunday and count those minutes.

In PHP that could look like this:

function isSunday(DateTimeInterface $dateTime) {
    return $dateTime->format('w') == 0;
}

function countSundayMinutes(DateTime $start, DateTime $end): int
{
    if ($start >= $end) {
        throw new LogicException('end must be > start!');
    }
    $sundayMinutes = 0;
    $current = clone $start;
    while ($current < $end) {
        if (isSunday($current)) {
            $sundayMinutes  ;
        }
        $current = $current->add(DateInterval::createFromDateString('1 minute'));
    }

    return $sundayMinutes;
}

echo countSundayMinutes(new DateTime('2023-01-02 00:00'), new DateTime('2023-01-03 00:00')), PHP_EOL; // 0: total 24h, not on sunday
echo countSundayMinutes(new DateTime('2023-01-01 12:00'), new DateTime('2023-01-01 13:00')), PHP_EOL; // 60: total 60 minutes, thereof 60 on sunday
echo countSundayMinutes(new DateTime('2022-12-31 23:00'), new DateTime('2023-01-01 01:00')), PHP_EOL; // 60: total 12 minutes, thereof 60 on sunday
echo countSundayMinutes(new DateTime('2022-12-31 00:00'), new DateTime('2023-01-03 00:00')), PHP_EOL; // 1440: total 72h, thereof 24h (1440 minutes) on sunday

But i'm sure you'll be able to add many optimizations to that algorithm, e.g. you could check first if the given period includes any sundays at all...

  •  Tags:  
  • php
  • Related