Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
|
Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 28th, 2003, 07:53 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default leap years & cal_days_in_month

I have a question for someone who is good with math.

I am trying to write my own version of the 'cal_days_in_month' function. Apparently my ISP doesn't have those functions enabled. Why I don't know.

My version doesn't require transition to a different callender. Where I am getting hung up is on the part that calculates whether the current year is a leap year. How would I find out if the current year is a factor of four? Like 4, 8, 16, 20, etc? Its been ages since I was in a math class but I'm sure there's an easy solution to it. I just need a little refresher.

Thanks in advance!
: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old September 28th, 2003, 09:55 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I remember it like this:

The year has to be evenly divisible by 4, but not 100, unless it's divisible by 400.

So what does that mean?

1880 - leap year, divisible by 4
1881 - not a leap year, not divisible by 4
1884 - leap year, divisible by 4
1900 - not a leap year, divisible by 100 but not by 400
2000 - leap year, divisible by 400

So basically, the formula is:

function is_leap_year($year)
{
    return (0 === ($year % 4)) &&
           ((0 !== ($year % 100)) || (0 === ($year % 400)));
}

In order, it reads:

If year is divisible by four AND either not divisible by 100 or divisible by 400, it's a leap year.


Take care,

Nik
http://www.bigaction.org/
 
Old September 28th, 2003, 09:57 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Oh yeah -- the answer to your question, "How can you tell if X is divisible by Y" is to use modulo division and check that the remainder is zero.

  http://www.php.net/operators.arithmetic


Take care,

Nik
http://www.bigaction.org/
 
Old September 29th, 2003, 01:02 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Thanks Nik!
Very helpful information!

: )

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old December 27th, 2003, 02:15 AM
blb blb is offline
Registered User
 
Join Date: Dec 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi, to check if a particular year is a

leap year or not you just need to

check 2 condition in sequence.

1) If the year is divisible by 400, it

is leap year and you do not need to

check the second condition.

2) If the above condidtion is not

satisfied, then check if the year is

divisible by 4 but not divisible by

100, then only it is a leap year else

it is not...

C coding..

If(year%400==0 ||

(year%4==0&&year%100!=0))
     printf("Leap Year");
else
     printf("Not a Leap Year");

Hey!!.. r u agreed upon it?

Cheer up
:)






Similar Threads
Thread Thread Starter Forum Replies Last Post
Leap Year Calculation in SSRS anibis.knowledge BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 0 February 28th, 2008 07:39 AM
adding years to date Israr JSP Basics 1 March 23rd, 2007 07:56 AM
DateDiff calculation for Years, Months & Days demivolt ASP.NET 1.0 and 1.1 Professional 6 October 14th, 2006 12:14 AM
Years to Months Conversion mahajanvit ASP.NET 2.0 Basics 2 September 25th, 2006 11:43 PM
Query by years anukagni Access 3 September 6th, 2006 06:11 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.