0

I want to calculate all Sunday's HOURS between two given dates.

I don't have any ideas

$start = new \DateTime($startDate);
$end = new \DateTime($endDate);
$days = $start->diff($end, true)->h;
$sundays = intval($days / 168) + ($start->format('N') + $days % 168 >= 168);
return $sundays;

This code only works with number of days and not hours of Sunday.

ramayac
  • 5,173
  • 10
  • 50
  • 58
  • 1
    If it works for the number of days (I guess you are talking about number of sundays in the interval), can't you just multiply it by 24? – Lucas Arbex Sep 17 '19 at 17:22
  • @LucasArbex Not really, because either start or end date can be in the middle of sunday. But this scenario can also be easly handled. – kaczmen Sep 17 '19 at 17:27
  • Oh, ok, got it. Let me see if I can come up with a solution here then... – Lucas Arbex Sep 17 '19 at 17:29
  • 1
    Have you seen this [question](https://stackoverflow.com/questions/15886166/calculate-sundays-between-two-dates). It can help you... – Lucas Arbex Sep 17 '19 at 17:34
  • @LucasArbex I know this question but it's just the number of days i want hours on sundays –  Sep 17 '19 at 17:45
  • Do you need to handle DST as well? Where I live, we have DST, and the transition is on Sundays. So every year, there is a 23, and a 25 hour long Sunday as well. – Nyos Sep 17 '19 at 19:33
  • what do you mean hours on Sundays? Surely count how many Sundays between two dates and * by 24? – treyBake Sep 17 '19 at 22:38

2 Answers2

0

Firstly I copied the solution from this question, and then I made an if statement to check if the final date $endDate is a Sunday. If it is, I added their hour count to the total hour/count in the interval.

function quantitySundays($startDate, $endDate)
{
    $start = new DateTime($startDate);
    $end = new DateTime($endDate);
    $days = $start->diff($end, true)->days;

    $sundays = intval($days / 7) + ($start->format('N') + $days % 7 >= 7);

    if($end->format('D') == 'Sun') {
        return $sundays * 24 + $end->format('H');
    } else {
        return $sundays * 24;
    }
}

The only modification is that you have to add H:i:s to the dates that you pass as parameter in the function, like so: '2019-09-15 14:00:00'.

Is this what you are looking for?

Lucas Arbex
  • 889
  • 1
  • 7
  • 15
0

I see you already got a good answer. I tried to solve it too, here's my code.

$d1 = new DateTime('2019-08-26');
$d2 = new DateTime('2019-09-02');

$first_day =  $d1->format('l');

$days = array(
    'Monday' => 6,
    'Tuesday' => 5,
    'Wednesday' => 4,
    'Thursday' => 3,
    'Friday' => 2,
    'Saturday' => 1,
    'Sunday' => 7,
);

$hours = 0;
$sunday_count = 0;

while($d1 <= $d2){

    if($days[$first_day] == 7){
        $hours += 24;
        $sunday_count++;

        $temp = '+'.$days[$first_day].' day';
        $d1->modify($temp);
        $first_day = $d1->format('l');

    }else{
        $temp = '+'.$days[$first_day].' day';
        $d1->modify($temp);
        $first_day = $d1->format('l');
        if($d1 <= $d2){
            $hours += 24;
            $sunday_count++;
        }else{
            break;
        }
    }
}

echo 'total hours : '.$hours.'<br>';
echo 'Sunday count: '.$sunday_count;
Umer Abbas
  • 1,866
  • 3
  • 13
  • 19