tags:

views:

444

answers:

5

from 01-01-2009 to 02-23-2009

how to find out how many Sunday in between these days??

+2  A: 

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 :

  • first, get the date of the first sunday after 2009-01-01
  • then, as long as the current date is before 2009-02-23, iterate
  • at each iteration, add 7*24*3600 seconds (7 days per week, 24 hours per day, 3600 seconds per hour)

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)

Pascal MARTIN
if my($dateMax) end day is a Sunday then it will show incorrect answer :(
coderex
If you want to include the $dateMax, and not exclude it, using <= instead of strict < in the condition of the while loop might help.
Pascal MARTIN
A: 

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.

Alex Shnayder
+2  A: 

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++;
af
A: 

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);
Nirmal
A: 

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

Dharma