Calculating the number of Saturdays and Sundays

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

    I don't think there is a built in for that, but this should do the job :

    $startTime = START_TIMESTAMP;
    $endTime = END_TIMESTAMP;
    $time = $startTime;
    $count = 0;
    
    while(date('w', $time) != 0) { // 0 (for Sunday) through 6 (for Saturday)
        $time += 86400;
    }
    
    while($time < $endTime) {
        $count++;
        $time += 7 * 86400;
    }
    

提交回复
热议问题