Page 1 of 1

Age from Birthday

Posted: Fri Oct 03, 2008 10:22 pm
by hmvrulz
i have create a script to get the age from Birthday. here it is...
If theres any easy method or logic please let me knw

Code: Select all

<?php
// Count number of days between two dates
function days($endDate, $beginDate)
{
 
                //explode the date by "-" and storing to array
                $date_parts1 = explode("-", $beginDate);
                $date_parts2 = explode("-", $endDate);
                //gregoriantojd() Converts a Gregorian date to Julian Day Count
                $start_date = gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
                $end_date = gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);
                return $end_date - $start_date;
}
 
 
// Age from birthday
function get_age($bday)
{
                $no_of_days = days(date("Y-m-d"), $bday);
                $years = $no_of_days / 365;
                $round_off = explode(".", $years);
                return $round_off[0];
 
            
}
 
echo get_age('1986-06-22');
 
?>
 
 

Re: Age from Birthday

Posted: Sat Oct 04, 2008 2:37 am
by jmut
Here what I'am using.

Code: Select all

 
//n*    arguments by default will give age as of now. If given you can see what age is in any given moment.
function getAgeByDate ($iMonth, $iDay, $iYear, $nMonth=0, $nDay=0, $nYear=0) {
    $now = time();
    date_default_timezone_set('Europe/Sofia');
    if (!$nMonth) $nMonth = date("m",$now);
    if (!$nDay) $nDay = date("d",$now);
    if (!$nYear) $nYear = date("Y",$now);
    $baseyear = $nYear - $iYear - 1;
    If ($iMonth < $nMonth OR ($iMonth == $nMonth AND $iDay <= $nDay)) {
        // had birthday
        $baseyear++;
    }
    return $baseyear;
}
 

Re: Age from Birthday

Posted: Sat Oct 04, 2008 3:19 am
by onion2k
If your date is coming from a database..

Code: Select all

SELECT FLOOR(DATEDIFF(CURDATE(), DATE('1977-03-28'))/365.25) AS age
Just replace DATE('1977-03-28') with the name of your date column.

Re: Age from Birthday

Posted: Sat Oct 04, 2008 11:29 pm
by hmvrulz
jmut wrote:Here what I'am using.

Code: Select all

 
//n*    arguments by default will give age as of now. If given you can see what age is in any given moment.
function getAgeByDate ($iMonth, $iDay, $iYear, $nMonth=0, $nDay=0, $nYear=0) {
    $now = time();
    date_default_timezone_set('Europe/Sofia');
    if (!$nMonth) $nMonth = date("m",$now);
    if (!$nDay) $nDay = date("d",$now);
    if (!$nYear) $nYear = date("Y",$now);
    $baseyear = $nYear - $iYear - 1;
    If ($iMonth < $nMonth OR ($iMonth == $nMonth AND $iDay <= $nDay)) {
        // had birthday
        $baseyear++;
    }
    return $baseyear;
}
 
this works with INDIA ?

i didnt find the Time ZONE code for this on php.net

Re: Age from Birthday

Posted: Mon Oct 06, 2008 6:41 am
by jmut
Remove date_default_timezone_set() call and try. It's just there cause it's strict warning.
Well, is India special when it comes to dates/calendar?

Re: Age from Birthday

Posted: Wed Oct 08, 2008 5:36 am
by hmvrulz
jmut wrote:Remove date_default_timezone_set() call and try. It's just there cause it's strict warning.
Well, is India special when it comes to dates/calendar?
No its usually +5.5 difference

Re: Age from Birthday

Posted: Wed Oct 08, 2008 5:44 am
by VladSun
Another DB query:
[sql]SELECT YEAR(FROM_DAYS(DATEDIFF(NOW(), DATE('1977-03-28'))));[/sql]