How to iterate over days?

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
Thresher
Forum Newbie
Posts: 13
Joined: Sun Apr 01, 2007 3:12 am

How to iterate over days?

Post by Thresher »

I want an iterator that will generate:
January 1, 1970,
January 2, 1970,
and so on…

In Oracle’s SQL, you can iterate over a range of days using the NEXT_DAY() or apply arithmetic operators to dates. Are there any similar functions in PHP?

You may think that since a day has 86400 seconds, that you can just use the multiples of 86400. There are a few problems with this approach but the killer is that leap seconds are necessary to keep UNIX time in synchronization with the Earth’s rotation. Don’t believe me? See http://en.wikipedia.org/w/index.php?tit ... ntable=yes. Therefore, there are lots of days, like March 5, 2007, whose UNIX time is not a multiple of 86400.

I want a set of functions where I don’t need to worry about leap years with February 29. If I have a date that says “February 28” for some year, I can just ask the iterator for the next date, and it will figure out if there is a leap year to worry about.

Any suggestions?

Thanks.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Code: Select all

$tomorrow = strtotime('February 28, 2007 +1 day');
Thresher
Forum Newbie
Posts: 13
Joined: Sun Apr 01, 2007 3:12 am

Thank you

Post by Thresher »

Thanks Kieran Huggins. That is exactly what I need.
Post Reply