
Solid, safe website tools.
Whatever your need, Will Bontrager builds powerful software solutions.
|
(Get Attention Ticker for your web site)
How To Calculate Leap Years with PerlBy Will Bontrager I prefer to use the localtime and timelocal functions when doing calendar routines. It's so much easier. However, these functions can only work on dates between December 31, 1969 and January 18, 2038. While programming the new Your Daily Lucky Numbers web page, the daily lucky numbers charts could span up to a year and include traversing from one year to the next. Although the site is free, I could not see restricting users to charts of years 1970 through 2037. So I made my own code. And I thought I would share the leap year calculation subroutine with you. First I'll present the code, then the rules for calculating which years are leap years, and finally the code again but with extensive comments interspersed. The Code
The IsLeapYear subroutine is called with a year number, like The function then returns a number 1 if the year number is a leap year. It returns a number 0 if it is not a leap year.
This line will provide the $February value with the correct number of days for the given year: The RulesIf the 4-digit year is not evenly divisible by 4, it is not a leap year. If it is evenly divisible by 4 and also evenly divisible by 100, it is not a leap year unless it is also evenly divisible by 400. Therefore:
1600 was a leap year. 1700, 1800, and 1900 were not leap years. 2000 was a leap year. The CommentsIt's a handy little subroutine. January 16, 2006 Please note: Articles on this website are presented "as is". However - If you have a question about a CGI script, HTML, CSS, PHP, or JavaScript
Rate this blog post.
No page reload! |
|
|
© 1998-2001 William and Mari Bontrager |
|