Jump to content

[SOLVED] Loop through days in a month?


Solarpitch

Recommended Posts

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',

Link to comment
https://forums.phpfreaks.com/topic/140818-solved-loop-through-days-in-a-month/
Share on other sites

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');
}  
?>

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';
}  
?>

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.