Jump to content

Date help - fetch dates for Mondays between xxx and yyy?


Recommended Posts

<?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 />";
}
?>

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 />";
}
?>

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 />";
}
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.