from 01-01-2009 to 02-23-2009
how to find out how many Sunday in between these days??
from 01-01-2009 to 02-23-2009
how to find out how many Sunday in between these days??
Hi,
What about something like this :
$date = strtotime('2009-01-01 next sunday');
$dateMax = strtotime('2009-02-23');
$nbr = 0;
while ($date < $dateMax) {
var_dump(date('Y-m-d', $date));
$nbr++;
$date += 7 * 24 * 3600;
}
var_dump($nbr);
And the ouput you get is :
string '2009-01-04' (length=10)
string '2009-01-11' (length=10)
string '2009-01-18' (length=10)
string '2009-01-25' (length=10)
string '2009-02-01' (length=10)
string '2009-02-08' (length=10)
string '2009-02-15' (length=10)
string '2009-02-22' (length=10)
For the dates of each sunday, and :
int 8
For the number of sundays
(I've quickly checked, and it seems those dates are indeed sundays)
What this code does is :
Note : I've changed the format of your dates, to use YYYY-MM-DD, and not DD-MM-YYY ; it is the format that's generally used -- so, probably a good idea to use this one, to be sure strtotime works properly. (not sure it'll work OK with your format)
You can calculate how many weeks are in between these two dates, there should be some date manipulation library in PHP, and then the number of Sundays will be the number of weeks, you still will need to see if first date is a Sunday or not, and last date is a sunday or not to deal with the edges of the date range.
Some examples can be seen here.
This should do the trick without any loops.
$start = mktime(0, 0, 0, $start_month, $start_day, $start_year);
$end = mktime(0, 0, 0, $end_month, $end_day, $end_year);
$days = ($end - $start) / 86400;
$sundays = $days / 7
// check if there are enough leftover days for one more sunday
if((localtime($start)[6]+($days % 7) > 6)
$sundays++;
Saw this somewhere here in SO and I used it in my project. Couldn't trace back the original post.
// function to calculate number of given day within date range
function number_of_days($day, $start, $end){
$w = array(date('w', $start), date('w', $end));
return floor( ( date('z', $end) - date('z', $start) ) / 7) + ($day == $w[0] || $day == $w[1] || $day < ((7 + $w[1] - $w[0]) % 7));
}
// define your dates
$start = mktime(0, 0, 0, 1, 16, 2010);
$end = mktime(0, 0, 0, 1, 25, 2010);
// display the count
echo number_of_days(0, $start, $end);
hi, its working fine,but i need to find out third sunday or forth sunday in between two days
for ex: statr Date: 2010-08-01 End Date: 2010-08-29
i need to display only 2010-08-15 please help me,
Thanks in advance