Search code examples
phphtmlcssbootstrap-modalleap-year

Leap year and non leap year into separate tables


This code will display the the leap year and non leap year from 1991 to 2100, I tried to make table one for leap year and other for non leap year but I failed.

How can I bring it in table format or in a grid system? This is for academic study.

 <!DOCTYPE html>
    <html>
    <head>
        <title>Leap Year</title>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    </head>
    <body>
        <?php  
        function isLeap($year) {
        return ((($year % 4) == 0) && ((($year % 100) != 0)));
        }

            for($year=1991; $year<=2100; $year++)  
            {  
                If (isLeap($year))  
                {   
                    $leap="$year : LEAP YEAR <br/>";
                    //echo "<div class='col-sm-12'>" . $leap . "</div>";
                    echo $leap;
                }  
                else  
                {  
                    $nonLeap="$year : Not leap year <br/>";  
                    //echo "<div class='col-sm-6'>" . $nonLeap ."</div>";       
                    echo $nonLeap;           
                }  
            }
        ?>
    </body>
    </html>

Solution

  • Your isLeap function is wrong. You also can refer to this post.

    function isLeap($year) {
        return ((($year % 4) == 0) && ((($year % 100) != 0) || (($year % 400) == 0)));
    }
    
    for($year=1991; $year<=2100; $year++)  
    {  
        if (isLeap($year))  
        {   
            $leaps[] = $year;
        }  
        else  
        {  
            $nonLeaps[] = $year;
        }
    }
    
    echo '<table><tr>';
    foreach($leaps as $y)
    {
      echo '<td>' . $y . '</td>';
    }
    echo '</tr></table>';