Solarpitch Posted January 14, 2009 Share Posted January 14, 2009 Hey Guys, Is there a function I can use that will loop through a month returning each day. I would need something like <?php //loop through $month array here? //then do... foreach($month as $day) { $links = array( $day => 'http://www.mysite.com/calender/2009/03/$month', ); } ?> EDIT: SO basically if I could pass in the month as a number... 1 for Jan, 2 for feb... etc thats what I would need. Then it would create my array, as above like... 1 => 'http://www.mysite.com/calender/2009/01/1', 2 => 'http://www.mysite.com/calender/2009/02/1', 3 => 'http://www.mysite.com/calender/2009/03/1', .... 31 => 'http://www.mysite.com/calender/2009/31/1', Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 14, 2009 Share Posted January 14, 2009 $date = mktime(0,0,0,3,1,2009); //The get's the first of March 2009 $links = array(); for($n=1;$n <= date('t',$date);$n++){ echo $n; } Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted January 14, 2009 Author Share Posted January 14, 2009 Thanks rhodesa, that works perfect. I might be a little picky here but is there a way to format the days coming from the loop to have a 0 before the single numbers... 01, 02, 03. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 14, 2009 Share Posted January 14, 2009 either with: print str_pad($n,2,'0',STR_PAD_LEFT); or if you are familiar with printf() printf('http://www.mysite.com/calender/2009/03/%02d',$n); Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted January 14, 2009 Author Share Posted January 14, 2009 Cheers. Just one more question before I potter along. My array knowledge is not fully flexed in php at the moment so there may be a reason as to why the links array keeps overwriting each value and it just ends up holding the last value in the loop... which in this case is the 31st 31 => 'http://www.mysite.ie/enterprise/index.php/enterprise/dashboard/2009/01/31'); To me this makes logical sense and should produce... 1 => 'http://www.mysite.com/calender/2009/01/1', 2 => 'http://www.mysite.com/calender/2009/02/1', 3 => 'http://www.mysite.com/calender/2009/03/1', .... 31 => 'http://www.mysite.com/calender/2009/31/1', <?php $date = mktime(0,0,0,3,1,$year); //The get's the first of March 2009 $links = array(); for($n=1;$n <= date('t',$date);$n++){ $links = array($n => 'http://www.mysite.ie/enterprise/index.php/enterprise/dashboard/'.$year.'/'.$n.'/1'); } ?> Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 14, 2009 Share Posted January 14, 2009 nope...you keep assigning a new array to $links each time. this is what you want: <?php $date = mktime(0,0,0,3,1,$year); //The get's the first of March 2009 $links = array(); for($n=1;$n <= date('t',$date);$n++){ $links[$n] = 'http://www.mysite.ie/enterprise/index.php/enterprise/dashboard/'.$year.'/'.$n.'/1'; } ?> Quote Link to comment Share on other sites More sharing options...
Solarpitch Posted January 14, 2009 Author Share Posted January 14, 2009 Thanks for you help. Quote Link to comment 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.