Forum Moderators: coopster

Message Too Old, No Replies

Fetching the date of every Monday

for the next 12 months

         

wheelie34

5:17 pm on Jan 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is it fairly easy to dynamically print the date of the next 12 months from the current day?

I can't get my head around the method needed the format I want is M d, Y

Where do I start? what methods do I use?

whoisgregg

5:40 pm on Jan 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The strtotime function [php.net] will accept a wide range of textual formats, including counts of particular day names like "+3 monday" (third monday from today) or "-2 friday" (two fridays ago). So, if you want the next 52 weeks worth of mondays, a simple loop will do the trick.

<?php
for($i=1; $i<=52; $i++){
echo date("M d Y", strtotime('+'.$i.' Monday')).'<br>';
}
?>

venelin13

5:42 pm on Jan 14, 2008 (gmt 0)

10+ Year Member



for($i = 0; $i < 12; $i++){
echo date("M d, Y", strtotime("+{$i} month"));
}

wheelie34

6:17 pm on Jan 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks guys

venelin13 your code isn't what I need, it prints the following 12 months only the day stays the same as the current day eg jan 14 feb 14 mar 14

whoisgregg: the code you posted appears to be exactly what I wanted but it just prints Dec 31 1969 52 times, the server I am working with has PHP 4.1 I think could that be the reason? is there a work around?

Thanks

whoisgregg

7:24 pm on Jan 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm... since venelin13's syntax worked for you, perhaps the quote type matters in your version of PHP?

Replace:

echo date("M d Y", strtotime('+'.$i.' Monday')).'<br>'; 

With:

echo date("M d Y", strtotime("+{$i} Monday")).'<br>'; 

coopster

7:31 pm on Jan 14, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



PHP 4.1 is 6 years out of date. There were quite a few bug fixes and enhancements to PHP since then. Time to upgrade!
[php.net...]

wheelie34

7:35 pm on Jan 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks again, I did try what you suggested but had the same result, I read up on the strintotime page and worked out it was the Monday it didn't like, heres what is almost perfect it just ignores next Monday and starts from the 28th the rest of the dates are the next 52 Mondays but wheres next Monday?

for($i=1; $i<=52; $i++){
echo date("M d Y", strtotime('next Monday +'.$i.' week')).'<br>';
}

Any idea?

Edit: Coopster it was a mistake its the last version of 4 I am told

Edit2: got it, changed the $i=1 to $i=0

coopster

7:55 pm on Jan 14, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The issue is still odd, if you ask me though. The very first example given by whoisgregg should have worked just fine.

wheelie34

9:32 am on Jan 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Coopster I agree it's odd. Yesterday was Monday and it showed next Monday the 21st, today it's dropped the 21 and is showing from the 28th onwards, I am populating a select with the dates heres my code so far.

print "<select name=\"start\" size=\"1\">";
print "<option value=\"Select Week\" selected>Select Week</option>";
for($i=0; $i<=52; $i++){
$num = date("d M Y", strtotime('next Monday +'.$i.' week'));
print"<option value=\"{$num}\">{$num}</option> \n";
}
print "</select>";

Any help/advice

wheelie34

10:28 am on Jan 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Looks sorted, heres what I ended up with and it seems correct

for($i=0; $i<=52; $i++){
date("d M Y", strtotime(' Monday +'.$i.' week'));