Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating the number of Saturdays and Sundays

Tags:

date

php

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

Is there any inbuilt function for that purpose?

like image 857
Satish Avatar asked Jun 02 '10 14:06

Satish


2 Answers

There is a related question here already, Calculate business days

You can use this to subtract from 7 to get the weekend days, or similar.

like image 156
David Yell Avatar answered Oct 09 '22 14:10

David Yell


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;
}
like image 28
Serty Oan Avatar answered Oct 09 '22 15:10

Serty Oan