473,405 Members | 2,279 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Determining if a year is a leap year

right, i've to write a small script to check if a year is leapyear or not. i understand that

a) the year or number when divided by 400, 100 and 4, should leave me with a module remainder of 0, correct?

so, if what i've said above is right.......... i wrote this script...... now i'm sure this is probably completly wrong, but i'm a bit new to all this.....

the script is being parsed to a html for by the way.........

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.  
  4. $leap = $x;
  5.  
  6.  
  7.  
  8. $x = $_POST["year"];
  9.  
  10. if ($leap == (($x%100) == 0 || ($x%400) == 0 || ($x%4) == 0)
  11.  
  12. {
  13. echo("this is a leap year");
  14. }
  15.  
  16.  
  17. else
  18. {
  19.  
  20. echo("this is not a leap year");
  21. }
  22.  
  23. ?>

i'm getting an error on line 12, which is just a curley brace, {

am i close here, of way off?

ta
Jul 25 '07 #1
15 8733
mwasif
802 Expert 512MB
You are missing ending ')' in if
[PHP]if ($leap == (($x%100) == 0 || ($x%400) == 0 || ($x%4) == 0))[/PHP]
Use this to know if the year is leap or not
[PHP]$year = 2005;
if(date("L", mktime(1, 0, 0, 1, 1, $year)) == 1)
echo "this is a leap year";
else
echo "this is not a leap year";[/PHP]
If you have PECL then you can use mcal_is_leap_year().
Jul 25 '07 #2
You are missing ending ')' in if
[PHP]if ($leap == (($x%100) == 0 || ($x%400) == 0 || ($x%4) == 0))[/PHP]
Use this to know if the year is leap or not
[PHP]$year = 2005;
if(date("L", mktime(1, 0, 0, 1, 1, $year)) == 1)
echo "this is a leap year";
else
echo "this is not a leap year";[/PHP]
If you have PECL then you can use mcal_is_leap_year().
thanks, yes, was missing a )

i don't really understand the code you wrote, would you be able to comment on my code?

the user has to put a year, just a number in a text box, and submit.

at the moment i'm only learning about Conditionals.

does my code make any sence?
Jul 25 '07 #3
ok, i figured this out, the code.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $x = $_POST["year"];
  4.  
  5.  
  6. $leap = $x;
  7.  
  8.  
  9. if ( ( ($x % 4 == 0) && ($x % 100 !=0) )  ||  ($x % 400 == 0) )
  10.  
  11. {
  12.         echo("this is a leap year");
  13. }
  14.  
  15.  
  16. else 
  17.  
  18.         echo("this is not a leap year");
  19. }
  20.  
  21. ?>
Jul 25 '07 #4
kovik
1,044 Expert 1GB
Why would you need that much logic to determine a leap year? Every four years is a leap year. Period.

FYI, PHP has date and time functions that can give you leap years and the like.
Jul 26 '07 #5
pbmods
5,821 Expert 4TB
Hey, go easy on him. He's doing homework. When does homework ever have to be practical?

date('L') does the same thing, but what would ballygowanboy learn from that?
Jul 26 '07 #6
kovik
1,044 Expert 1GB
Hey, go easy on him. He's doing homework. When does homework ever have to be practical?

date('L') does the same thing, but what would ballygowanboy learn from that?
Hehe, I was just implying that x%4 is all he needs, and the rest is unnecessary. In fact, the logic says that every four years is a leap year except for every 100 years, unless it is every 400 years? So, only one of those would be a leap year per 4 centuries? It doesn't really make sense.
Jul 26 '07 #7
nathj
938 Expert 512MB
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $x = $_POST["year"];
  4.  
  5.  
  6. $leap = $x;
  7.  
  8. ?>
why define $x and $leap the same and only use one of them? Perhaps it would be better, and this has no bearing on the lopo issue at all, if you only had one variable - $leap. Define this directly from the $_POST['year'] and save some processing cycles.

Just thought I'd add my 2cents worth
nathj
Jul 26 '07 #8
pbmods
5,821 Expert 4TB
Changed thread title to better describe the problem.
Jul 26 '07 #9
Quote: "In fact, the logic says that every four years is a leap year except for every 100 years, unless it is every 400 years? So, only one of those would be a leap year per 4 centuries? It doesn't really make sense."

That actually is the rule to keep the seasons from slowly drifting.

1900 was not a leap year
2000 WAS a leap year
2100 will not be a leap year

Of course not too many PHP programmers need to worry. The UNIX timestamp function will roll its 32 bits in about 2027, I think, so a lot programs will fall over.

A nice little earner for the clean up crews.
Jul 26 '07 #10
kovik
1,044 Expert 1GB
That actually is the rule to keep the seasons from slowly drifting.
Seriously? That's kinda cool. I didn't know that.

Of course not too many PHP programmers need to worry. The UNIX timestamp function will roll its 32 bits in about 2027, I think, so a lot programs will fall over.

A nice little earner for the clean up crews.
Well, we'll come up with a alternative to timestamps by then, and it'll be forced into the date() function in PHP 9. ;)
Jul 27 '07 #11
why define $x and $leap the same and only use one of them? Perhaps it would be better, and this has no bearing on the lopo issue at all, if you only had one variable - $leap. Define this directly from the $_POST['year'] and save some processing cycles.

Just thought I'd add my 2cents worth
nathj
you're right, took that out, was left over code from a couple of attempts.
Jul 27 '07 #12
Why would you need that much logic to determine a leap year? Every four years is a leap year. Period.

FYI, PHP has date and time functions that can give you leap years and the like.

Every fourth year is a leap year - 1996, 2004 and 2008 except

Years ending in 00 are not leap - 1900, 1800, 1700 are not leap years but

Every 400 years there is a leap year ending in 00 (1600 and 2000 are leap years)


also, i'm only learning, so need to understand the basic principals of programming.
Jul 27 '07 #13
nathj
938 Expert 512MB
also, i'm only learning, so need to understand the basic principals of programming.
That's a great idea - get the basics in place and the rest will follow.

A good understanding of the basics will make you a better programmer in the long run.

nathj
Jul 27 '07 #14
kovik
1,044 Expert 1GB
That's a great idea - get the basics in place and the rest will follow.

A good understanding of the basics will make you a better programmer in the long run.

nathj
Yeah. It seems like everyone else just jumps in trying to make YouTube or Facebook from scratch. :-p
Jul 27 '07 #15
pbmods
5,821 Expert 4TB
I think this question has been sufficiently answered. I'm closing this thread.
Jul 28 '07 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Joshua Beall | last post by:
Hi All, I am wondering what the best way to determine an age of something/somone is. For instance, let's say I have two dates: 1985-07-19 2003-11-30 Now, I want to determine the interval...
4
by: John Hunter | last post by:
>>> from datetime import date >>> dt = date(1005,1,1) >>> print dt.strftime('%Y') Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: year=1005 is before 1900; the...
2
by: owz | last post by:
Ok, I am making a program (java class file) 2 work out if a date entered is valid or invalid for the day, month year, and for leap years.. dd/mm/yyyy . I seem 2 have gotten it 2 validate the year...
8
by: rn216_ccc | last post by:
Hi there all, I found a web site,. http://www.onlineconversion.com/leapyear.htm, where it can give a list of leap years by within the given range by the user, or the user may enter a year and the...
8
by: SONIQ | last post by:
You can determine whether a year is a leap year by testing if it is divisible by 4. However, years that are also divisible by 100 are not leap years, unless they are also divisible by 400, in which...
12
by: Brigitte Behrmann | last post by:
Can anyone assist with my code. I just cannot get this to work: I have to write a script that allows the user to enter a year & then determine whether it is a leap year or not. The request is for a...
37
by: mazwolfe | last post by:
I'm new here, so excuse me if my style is incorrect. Can anyone come up with a better method for this calculation? Code: int is_leap(int year) { switch (year % 19) { case 0: case 3: case 6:...
5
by: jimix | last post by:
here's what i have. using microsoft studio #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> int main( void ) { int year, b, c, e; b = 4; c = 100;
4
by: jrw133 | last post by:
Hello. So im a little bit stuck on one of my homework questions. heres the question: Write a C Shell Script that performs the following functions. Asks the user to input a year. The script...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.