I work on our soccer teams pages and I need a way to calculate players age in years. I managed to calculate players ages that are born after Unix epoc time (1970...?) but my genious algorithm chokes with dates before that.

so I have something like:

$born = mktime(0,0,0,$month, $day, year$)
$now = time ()
$age = bcdiv (($now - $born)/31536000)

also as a feature I would appreciate a snippet that checks if a person has a birthday today. It must be something very simple I think...

And no this isn't a school project :-)

Panu

    Well I can't help with the first bit, but checking if they have a birthday today is easy.

    Just get the date for today and compare it to their birthday, lets assume you get the variable $birthday out of the db and you then get todays date, make sure they're both in the same format

    dd/mm/yyyy

    and you have todays date as $date.

    if ($birthday == $date) {do whatever;}

    Hope that makes sense

      I'm not at a machine where I can test this so I don't know if it will work:

      Could you format NOW() and the Birthdate as YYMM and then apply the PERIOD_DIFF function to get the number of months between the dates?

      Does the following work?

      SELECT PERIOD_DIFF(DATE_FORMAT(NOW(), '%y%m'), DATE_FORMAT(Birthdate, '%y%m')) / 12

        8 months later

        For who it may help...


        You can calculate age using the code below. This code accounts for the UNIX Epoch and leap years. All you need to do is provide it a date in the m/d/yyyy format.

        <?php
        $birthday = "7/4/1960"; //must be as m/d/yyyy

        $bday = explode("/", $birthday); //parse
        $b_mm = $bday[0]; //birthday month
        $b_dd = $bday[1]; //birthday day
        $b_yyyy = $bday[2]; //birthday year

        //compare timestamps of mm/dd for birthday and today
        $bday_mm_dd = mktime(0,0,0,$b_mm,$b_dd,0);
        $today_mm_dd = mktime(0,0,0,date("m"),date("d"),0);
        $age = date("Y", time()) - $b_yyyy;

        if ($bday_mm_dd > $today_mm_dd) {
        //birthday hasn't happened yet this year
        $age = $age - 1;
        }

        print $age; //age equals 41 (in this case)
        ?>

        Of course this code assumes the client and server machines exist in the same day.

          Regarding my previous note on calculating age...

          Because of strange possibilities mentioned (due to varying platforms, servers, versions etc.), I would improve the previous age calculating code in 2 ways:

          1. Go ahead and plug in a safe year (any 4-digit year above 1970) for the two usages of mktime instead of zero. Just make sure the years are the same.

          2. Go ahead and make sure that when you setup the $bday_mm_dd and $today_mm_dd variables that you use the same format regarding leading zeros. In other words, if $bday uses leading zeros (e.g. 07/04/1960), use date("m") and date("d") when assinging $today_mm_dd. If $bday does not use leading zeros (e.g. 7/4/1960), you better use date("n") and date("j") when assigning $today_mm_dd.

          That's all -- just to be safe!

            Write a Reply...