Jump to content

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


benphp

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.