benphp Posted January 28, 2010 Share Posted January 28, 2010 Does anyone know the best way to do this? I have two dates, say: 2010-01-26 and 2010-05-30 and I want to return the dates of all the Mondays between them: 2010-02-01 2010-02-08 2010-02-15 ....etc. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/190164-date-help-fetch-dates-for-mondays-between-xxx-and-yyy/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 28, 2010 Share Posted January 28, 2010 <?php // find all dates of a specific day of week (monday) $day = 'Monday'; // which day name to find $start = '2010-01-26'; // starting date $end = '2010-05-30'; // ending date $date = date('Y-m-d',strtotime("$start $day")); // get the first date matching the day to be found // check if the first matching date is <= the $end date if($date<=$end){ $array = array(); // array to hold the results while($date <= $end){ $array[] = $date; // store the date $date = date('Y-m-d',strtotime("$date + 1 week")); } echo "These are the matching dates:"; echo "<pre>",print_r($array,true),"</pre>"; } else { echo "There are no matching dates between: $start and $end<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/190164-date-help-fetch-dates-for-mondays-between-xxx-and-yyy/#findComment-1003327 Share on other sites More sharing options...
benphp Posted January 28, 2010 Author Share Posted January 28, 2010 Thanks! Yours is better than mine. I ended up doing it this way: <?php $startWeek = date("W", mktime(0,0,0,$smonth,$sday,$syear)) * 1; $endWeek = date("W", mktime(0,0,0,$emonth,$eday,$eyear)) * 1; //End Week Number $endWeek = $endWeek - $startWeek; //number of weeks out for($i=$startWeek; $i<=$endWeek; $i++){ print date("Y-m-d", strtotime(' Monday +'.$i.' week')) . "<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/190164-date-help-fetch-dates-for-mondays-between-xxx-and-yyy/#findComment-1003329 Share on other sites More sharing options...
benphp Posted January 29, 2010 Author Share Posted January 29, 2010 BTW, mine didn't work, so I used yours THANKS! <?php function get_mydates($day, $start, $end) { // find all dates of a specific day of week (monday) $date = date('Y-m-d',strtotime("$start $day")); // get the first date matching the day to be found // check if the first matching date is <= the $end date if($date<=$end){ $array = array(); // array to hold the results while($date <= $end){ $array[] = $date; // store the date $date = date('Y-m-d',strtotime("$date + 1 week")); } //echo "These are the matching dates:"; //echo "<pre>",print_r($array,true),"</pre>"; return $array; } else { echo "There are no matching dates between: $start and $end<br />"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/190164-date-help-fetch-dates-for-mondays-between-xxx-and-yyy/#findComment-1003375 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.