Age from Birthday

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hmvrulz
Forum Newbie
Posts: 20
Joined: Fri Oct 03, 2008 10:20 pm

Age from Birthday

Post 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');
 
?>
 
 
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Re: Age from Birthday

Post 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;
}
 
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Age from Birthday

Post 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.
hmvrulz
Forum Newbie
Posts: 20
Joined: Fri Oct 03, 2008 10:20 pm

Re: Age from Birthday

Post 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
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Re: Age from Birthday

Post 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?
hmvrulz
Forum Newbie
Posts: 20
Joined: Fri Oct 03, 2008 10:20 pm

Re: Age from Birthday

Post 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
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Age from Birthday

Post by VladSun »

Another DB query:
[sql]SELECT YEAR(FROM_DAYS(DATEDIFF(NOW(), DATE('1977-03-28'))));[/sql]
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply