Page 1 of 1

Date looping

Posted: Tue Jul 11, 2006 8:24 am
by b00ker_b0y
Hey, wondering if anyone can help me here.

After a function that will loop through a given year, print every date in that year (yyyy/mm/dd) to the screen with correct days in month (cattering for leap years too).

i.e.

2006

01/01/2006
02/01/2006
..
..
31/12/2006

with leap years showing the 29th day in feb.

Any ideas where to loop would be very helpful.

Thanks!

Posted: Tue Jul 11, 2006 8:32 am
by MarK (CZ)
You're asking how to loop it?
Just get the first day of the year

Code: Select all

$time = mktime(12,0,0,1,1,2006);
then increment it by one day in a loop

Code: Select all

$time += 24*3600;
and print out the day as you want

Code: Select all

echo date("r", $time);
ex:

Code: Select all

$year = 2006;
$time = mktime(12,0,0,1,1,$year);

while (date("Y", $time) == $year) {
  echo date("r", $time)."<br />\r\n";
  $time += 24*3600;
}
Or did you ask about something else?

Looping

Posted: Tue Jul 11, 2006 8:41 am
by b00ker_b0y
Yeah, sorry quite new to all this.

I just wanted to loop through every date in a given year. But i cant loop through 365 times as leap years I will need to loop through 366.

Could be confusing myself tho.

Thanks

Posted: Tue Jul 11, 2006 8:46 am
by b00ker_b0y
Thanks, just tried your code. But how would i get it in the following formate only yyyy-mm-dd? not familiar with this mktime function.

Posted: Tue Jul 11, 2006 10:12 am
by jamiel

Posted: Tue Jul 11, 2006 10:23 am
by MarK (CZ)
It's not in the mktime() function but in date() (check jamiels link).

Code: Select all

echo date("Y-m-d", $time);
should do the trick.

thanks

Posted: Wed Jul 12, 2006 5:54 am
by b00ker_b0y
cheers people. thanks for the help.