Calculating the number of Saturdays and Sundays

前端 未结 7 698
盖世英雄少女心
盖世英雄少女心 2020-12-17 20:23

How can I calculate the number of Saturdays and Sundays between two dates in php?

Is there any inbuilt function for that purpose?

7条回答
  •  粉色の甜心
    2020-12-17 20:51

    diff($d1);
    $number_of_days = $interval->format("%d");
    
    $number_of_weekends = $number_of_days / 7;
    $remainder = $number_of_days % 7;
    
    if ($remainder >=2 && $d1->format("D") == "Sat")
        $number_of_weekends++;
    elseif ($d1->format("w") + $remainder >= 8)
        $number_of_weekends++;
    

    I may have missed by one in the last condition, be sure to check it with different starting dates. (Feel free to edit this answer if you spot an error).

提交回复
热议问题