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......... -
<?php
-
-
-
$leap = $x;
-
-
-
-
$x = $_POST["year"];
-
-
if ($leap == (($x%100) == 0 || ($x%400) == 0 || ($x%4) == 0)
-
-
{
-
echo("this is a leap year");
-
}
-
-
-
else
-
{
-
-
echo("this is not a leap year");
-
}
-
-
?>
i'm getting an error on line 12, which is just a curley brace, {
am i close here, of way off?
ta
15 8733
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().
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?
ok, i figured this out, the code. -
<?php
-
-
$x = $_POST["year"];
-
-
-
$leap = $x;
-
-
-
if ( ( ($x % 4 == 0) && ($x % 100 !=0) ) || ($x % 400 == 0) )
-
-
{
-
echo("this is a leap year");
-
}
-
-
-
else
-
{
-
-
echo("this is not a leap year");
-
}
-
-
?>
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.
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?
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.
-
<?php
-
-
$x = $_POST["year"];
-
-
-
$leap = $x;
-
-
?>
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
Changed thread title to better describe the problem.
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.
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. ;)
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.
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.
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
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
I think this question has been sufficiently answered. I'm closing this thread.
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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:...
|
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;
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |