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

前提是你 提交于 2019-12-05 15:30:49
$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 $mondays array.

Addendum

Be aware that +86400 could lead to inconsistent result due to daylight saving. If your app is mission-critical better use +1 day

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

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('d-m-Y',mktime(0,0,0,date('m'),date('d')+$i,date('y'))),'<br/>';
    }
}

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!