I need to output a list of dates (only Mondays and Tuesdays) for the next 12 months from current date like so:
Jan 2010
Tue 12 Jan 2010
Mon 18 Jan 2010
Tue 19 Jan 2010
Mon 25 Jan 2010
Feb 2010
Tue 02 Feb 2010
Mon 08 Feb 2010
Tue 09 Feb 2010
Mon 15 Feb 2010
Tue 16 Feb 2010
Mon 22 Feb 2010
Mar 2010
Tue 09 Mar 2010
Mon 15 Mar 2010
Tue 16 Mar 2010
...
Being new to PHP I figured strtotime and looping over the next 52 weeks is the best way to go.
$blockedDatesInput = "08 Mar 2010,12 Apr 2010"; // dont show these dates
$blockedDates = explode ("," , $blockedDatesInput); // convert to array
$currentMonth = ""; // current month marker
// loop over the next 52 weeks to find Mondays and Tuesdays
for($i=0; $i<=52; $i++){
// build the month header
$monthReference = date("M Y", strtotime('+'.$i.' Week'));
// check if date exists in $blockeddate
if (!in_array(date("d M Y", strtotime('+'.$i.' Monday')), $blockedDates) ||
!in_array(date("d M Y", strtotime('+'.$i.' Tuesday')), $blockedDates) ) {
// check if we have to show a new month
if(strcmp($monthReference, $currentMonth) <> 0){
echo $monthReference.'<br />',"
";
}else{
// output the dates
echo date("D d M Y", strtotime('+'.$i.' Monday')).'<br />',"
";
echo date("D d M Y", strtotime('+'.$i.' Tuesday')).'<br />',"
";
}
$currentMonth = date("M Y", strtotime('+'.$i.' Week'));
}
}
However the output from my code is
Jan 2010
Mon 18 Jan 2010
Tue 12 Jan 2010
Mon 25 Jan 2010
Tue 19 Jan 2010
Feb 2010
Mon 08 Feb 2010
Tue 02 Feb 2010
Mon 15 Feb 2010
Tue 09 Feb 2010
Mon 22 Feb 2010
Tue 16 Feb 2010
Mar 2010
Mon 08 Mar 2010
Tue 02 Mar 2010
Mon 15 Mar 2010
Tue 09 Mar 2010
Mon 22 Mar 2010
Tue 16 Mar 2010
Mon 29 Mar 2010
Tue 23 Mar 2010
As you can see the dates are not in the right order and I am at a loss where I am going wrong here.
Is there a more elegant / simple way to solve this?
Version of PHP used is 5.2.11 and no prospect of going to 5.3 anytime soon :-(
Thanks for your help.
Code below modification as suggested by Aly.
Changed the computer date from Tue, 12/01/2010 to Wed, 13/01/2010 to test the output.
$blockedDatesInput = "08 Mar 2010,12 Apr 2010"; // dont show these dates
$blockedDates = explode ("," , $blockedDatesInput); // convert to array
$currentMonth = ""; // current month marker
// loop over the next 52 weeks to find Mondays and Tuesdays
for($i=0; $i<=52; $i++){
// build the month header
$monthReference = date("M Y", strtotime('+'.$i.' Week'));
// check if date exists in $blockeddate
if (!in_array(date("d M Y", strtotime('+'.$i.' Monday')), $blockedDates) ||
!in_array(date("d M Y", strtotime('+'.$i.' Tuesday')), $blockedDates) ) {
// check if we have to show a new month
if(strcmp($monthReference, $currentMonth) <> 0){
echo $monthReference.'<br />',"
";
}else{
// output the dates (changed the order as suggested by Aly)
echo date("D d M Y", strtotime('+'.$i.' Tuesday')).'<br />',"
";
echo date("D d M Y", strtotime('+'.$i.' Monday')).'<br />',"
";
}
$currentMonth = date("M Y", strtotime('+'.$i.' Week'));
}
}
Output again in the wrong order.
Jan 2010
Tue 19 Jan 2010
Mon 18 Jan 2010
Tue 26 Jan 2010
Mon 25 Jan 2010
Feb 2010
Tue 09 Feb 2010
Mon 08 Feb 2010
Tue 16 Feb 2010
Mon 15 Feb 2010
Tue 23 Feb 2010
Mon 22 Feb 2010
See Question&Answers more detail:
os