Calculating the number of Saturdays and Sundays

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

    I searched for awhile for a simple solution that worked and decided to write my own and came up with this

            $start = date('Y-m-d');
            $end = date('Y-m-d', strtotime($start.' +1 year'));
            $current = $start;
            $count = 0;
    
            while($current != $end){
                if(date('l', strtotime($current)) == 'Saturday'){
                    $count++;
                }
    
                $current = date('Y-m-d', strtotime($current.' +1 day'));
            };
    
            echo $count;
    
    0 讨论(0)
提交回复
热议问题