How to extract every Monday and every two weeks of Monday from a range of two dates in PHP?

I use the infamous jQuery UI Datepicker, and in my form I select a range of two dates.

First, the starting date is displayed, and the other is the end date.

Now I need to find algorthm, some tips and directions or helpers for calculating every Monday between the two dates.

For instance:

start: 2011-06-01 end: 2011-06-30 

Take me these 4 (four) dates that are posted on Mondays:

 1st: 2011-06-06 2nd: 2011-06-13 3rd: 2011-06-20 4th: 2011-06-27 

How could I achieve this?

And also, I will need every two weeks on Monday:

The result for two weeks should be:

 1st: 2011-06-06 2rd: 2011-06-20 
+4
source share
2 answers
 $start = strtotime('2011-06-01'); $end = strtotime('2011-06-30'); $mondays=array(); while( $start <= $end ) { if ( date('N',$start)==1 ) $mondays[]=$start; $start += 86400; //> incrementing one day //> i could have used strtotime('+1 day') but math sum is 10x faster } //> Untested 

Now you have all your Mondays in the $mondays .

Adding

Remember that +86400 may lead to inconsistent results due to daylight saving time. If your application is critical, use +1 day

strtotime('2010-10-31') + 86400 returns 2010-10-31

+6
source
 function getAllMondays($from_date, $to_date){ // getting number of days between two date range. $number_of_days = count_days(strtotime($from_date),strtotime($to_date)); for($i = 1; $i<=$number_of_days; $i++){ $day = Date('l',mktime(0,0,0,date('m'),date('d')+$i,date('y'))); if($day == 'Monday'){ echo Date('dm-Y',mktime(0,0,0,date('m'),date('d')+$i,date('y'))),'<br/>'; } } 

}

+3
source

All Articles