Leap year(s) in five year period

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
JonathanS
Forum Newbie
Posts: 2
Joined: Thu Feb 28, 2008 5:23 pm

Leap year(s) in five year period

Post by JonathanS »

Hope someone can help me. User defines two dates(dd/mm/yy, then want to display 1) Difference in days between the two inputs in dd/mm/yy and 2) the difference in days between the dates. The problem is I always want to show the difference i dd/mm/yy no matter how long the period is but the maximun value I want to show for days is 1825 or 1826(which is five years depending on if it´s one or two leapyears in the given period). Please help. Give me some hlep how to do the second bit. How do I know to stop on 1825 or 1826.

Thank you so much!
User avatar
Peter Anselmo
Forum Commoner
Posts: 58
Joined: Wed Feb 27, 2008 7:22 pm

Re: Leap year(s) in five year period

Post by Peter Anselmo »

I'm a little hazy on your question, I think timestamps will help you out. Use the mktime() function to convert both dates to unix timestamps (which takes into account leap years). Then find the difference, and divide by the number of seconds in a day.
JonathanS
Forum Newbie
Posts: 2
Joined: Thu Feb 28, 2008 5:23 pm

Re: Leap year(s) in five year period

Post by JonathanS »

date('L', mktime(12, 0, 0, 1,1,'2008'));

$startYear = substr($_POST['from_yr'], -2);
$endYear = substr($_POST['to_yr'], -2);

$leaps = 0;
for($i=$startYear; $i<=$endYear; $i++) {
if(date('L', mktime(12,0,0,1,1,$i))) $leaps++;
}

If i input 28 february 2008 - 28 february 2013 i get 5 years 0 monts and 0 days AND 1827 days and one leap years.
If i input 3 march 2008 - 3 march 2013 i get 5 years 0 monts and 0 days AND 1826 days put only one leap year. <-- Here I only want it to get one leap year.

Please help.

Thank you!
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Leap year(s) in five year period

Post by Benjamin »

A quick search for date_diff on php.net: http://us2.php.net/manual/en/ref.datetime.php#80866

Although I would say the best solution is to use MySQL's built in datediff: http://dev.mysql.com/doc/refman/5.0/en/ ... n_datediff
Post Reply