Search code examples
phpdatedate-difference

Algorithm for getting the Age and the leap years since birth


calculate the total number of leap years IN DAYS since his or her birthyear.

sample input from user: 1993 and the output should be : 2562

update:

here's my code so far, i created an algorithm if the specific year is a leap year.

if($yr%4 == 0) {

 $leaps++;

}

thinking about making a loop starting from 1993 and check every year if its a leap year.

update2 : solved. here's what I did. thanks guys

<?php 

    $dt = date("Y");

    $nm = $_POST['name'];
    $yr = $_POST['year'];
    $leaps = 0;
    $total = 0;
    $age = $dt - $yr;

    for($i = $yr; $i < $dt ; $i++) {

        if($i %4 == 0) {

            $leaps++;

        }

        $total = 366 * $leaps;
    }

    echo "Hi $nm, you are $age years old. There were $total since your birthday";   

?>


Solution

  • here is what you are looking for:

    <?php
    
         $now = time(); // or your date as well
         $your_date = strtotime("1993-01-01");
         $datediff = $now - $your_date;
         echo floor($datediff/(60*60*24));
    
    ?>