# Help Needed - Only "basic" PHP required.

• Feb 24, 2009, 23:53
j_irwin
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
logic_earth
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
crmalibu
Were you taught about the modulus operator? If so, I bet they want to see you use that here.
Code:

```if ((\$number &#37; 100) == 0) {     // evenly divisible by 100 }```
• Feb 25, 2009, 00:17
j_irwin
Quote:

Originally Posted by crmalibu
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 }```

yeah we were taught about the modulus operator. I just don't know how to put all of it together.
• Feb 25, 2009, 00:38
hiddenpearls
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
uniqueumang
Quote:

Originally Posted by hiddenpearls
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

Just want to polish your pseudo-code .Make sure there is odd number of truth condition. btw % is very important operator.