Calculating the number of Saturdays and Sundays

前端 未结 7 696
盖世英雄少女心
盖世英雄少女心 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:55

    Let us all KISS (Keep It Simple Stupid). Why make it so complicated?

    function countWeekendDays($start, $end)
    {
        // $start in timestamp
            // $end in timestamp
    
    
        $iter = 24*60*60; // whole day in seconds
        $count = 0; // keep a count of Sats & Suns
    
        for($i = $start; $i <= $end; $i=$i+$iter)
        {
            if(Date('D',$i) == 'Sat' || Date('D',$i) == 'Sun')
            {
                $count++;
            }
        }
        return $count;
       }
    

提交回复
热议问题