Date looping

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
b00ker_b0y
Forum Newbie
Posts: 10
Joined: Thu Jun 22, 2006 7:16 am

Date looping

Post 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!
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Post 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?
b00ker_b0y
Forum Newbie
Posts: 10
Joined: Thu Jun 22, 2006 7:16 am

Looping

Post 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.
b00ker_b0y
Forum Newbie
Posts: 10
Joined: Thu Jun 22, 2006 7:16 am

Thanks

Post 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.
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post by jamiel »

User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Post 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.
b00ker_b0y
Forum Newbie
Posts: 10
Joined: Thu Jun 22, 2006 7:16 am

thanks

Post by b00ker_b0y »

cheers people. thanks for the help.
Post Reply