Determining if a year is a leap year

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ballygowanboy
    New Member
    • Jul 2007
    • 47

    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.........

    [code=php]
    <?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");
    }

    ?>[/code]


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

    am i close here, of way off?

    ta
  • mwasif
    Recognized Expert Contributor
    • Jul 2006
    • 802

    #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_ye ar().

    Comment

    • ballygowanboy
      New Member
      • Jul 2007
      • 47

      #3
      Originally posted by mwasif
      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_ye ar().
      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?

      Comment

      • ballygowanboy
        New Member
        • Jul 2007
        • 47

        #4
        ok, i figured this out, the code.

        [code=php]
        <?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");
        }

        ?>[/code]

        Comment

        • kovik
          Recognized Expert Top Contributor
          • Jun 2007
          • 1044

          #5
          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.

          Comment

          • pbmods
            Recognized Expert Expert
            • Apr 2007
            • 5821

            #6
            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?

            Comment

            • kovik
              Recognized Expert Top Contributor
              • Jun 2007
              • 1044

              #7
              Originally posted by pbmods
              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.

              Comment

              • nathj
                Recognized Expert Contributor
                • May 2007
                • 937

                #8
                Originally posted by ballygowanboy
                [code=php]
                <?php

                $x = $_POST["year"];


                $leap = $x;

                ?>[/code]
                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

                Comment

                • pbmods
                  Recognized Expert Expert
                  • Apr 2007
                  • 5821

                  #9
                  Changed thread title to better describe the problem.

                  Comment

                  • ken205726
                    New Member
                    • Jul 2007
                    • 2

                    #10
                    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.

                    Comment

                    • kovik
                      Recognized Expert Top Contributor
                      • Jun 2007
                      • 1044

                      #11
                      Originally posted by ken205726
                      That actually is the rule to keep the seasons from slowly drifting.
                      Seriously? That's kinda cool. I didn't know that.

                      Originally posted by ken205726
                      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. ;)

                      Comment

                      • ballygowanboy
                        New Member
                        • Jul 2007
                        • 47

                        #12
                        Originally posted by nathj
                        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.

                        Comment

                        • ballygowanboy
                          New Member
                          • Jul 2007
                          • 47

                          #13
                          Originally posted by volectricity
                          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.

                          Comment

                          • nathj
                            Recognized Expert Contributor
                            • May 2007
                            • 937

                            #14
                            Originally posted by ballygowanboy
                            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

                            Comment

                            • kovik
                              Recognized Expert Top Contributor
                              • Jun 2007
                              • 1044

                              #15
                              Originally posted by 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

                              Comment

                              Working...