SitePoint Sponsor |
|
User Tag List
Results 1 to 6 of 6
-
Feb 24, 2009, 23:53 #1
- Join Date
- Jun 2008
- Posts
- 39
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Help Needed - Only "basic" PHP required.
Hi All,
I've just started a class on PHP for my TAFE course and I'm stuck already. We haven't done much PHP as of yet - learnt about if/else statements, switch statements and some loops but that is all. Now I have a task to do involving determining whether a year is a leap year or not. It does not have user input, just declare $year inside the code.
I've been able to determine whether $year is a leap year when dividing by 4 but am unable to take it more advanced. Part A said to use if/else statements. This is part b (which I'm stuck on):
If a year is evenly divisible by 4, it is a leap year, unless it is divisible by 100. if it is divisible by 100, it is not a leap year unless it is also evenly divisible by 400. if the year is not a leap year, print the number of years to the next leap year.
Anyone have any suggestions?
Thanks.
-
Feb 25, 2009, 00:04 #2
to find the next leap year, when you find one it is simple as adding 4.
PHP Code:var_dump( 2008 + 4 );
-
Feb 25, 2009, 00:16 #3
- Join Date
- Jul 2008
- Posts
- 5,757
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Were you taught about the modulus operator? If so, I bet they want to see you use that here.
Code:if (($number % 100) == 0) { // evenly divisible by 100 }
-
Feb 25, 2009, 00:17 #4
- Join Date
- Jun 2008
- Posts
- 39
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Feb 25, 2009, 00:38 #5
- Join Date
- Dec 2007
- Location
- Pakistan
- Posts
- 262
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
to determine whether a year is a leap year or not , following are algorithms and pseudo code . hope this gives better understanding !
if year modulo 400 is 0 then leap
else if year modulo 100 is 0 then no_leap
else if year modulo 4 is 0 then leap
else no_leap
A more direct algorithm (terms may be grouped either way):
function isLeapYear (year):
if ((year modulo 4 is 0) and (year modulo 100 is not 0)) or (year modulo 400 is 0)
then true
else false
-
Feb 25, 2009, 03:47 #6
- Join Date
- Nov 2007
- Posts
- 63
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Umang Parikh
http://www.technotrack.co.nz/
Bookmarks