hmvrulz Posted October 4, 2008 Share Posted October 4, 2008 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 <?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'); ?> Link to comment https://forums.phpfreaks.com/topic/126974-age-from-birthday/ Share on other sites More sharing options...
F1Fan Posted October 4, 2008 Share Posted October 4, 2008 Just use strtotime(). <?php $start = '1986-09-22'; $end = date('Y-m-d); $seconds = strtotime($end)-strtotime($start); ?> Link to comment https://forums.phpfreaks.com/topic/126974-age-from-birthday/#findComment-656814 Share on other sites More sharing options...
hmvrulz Posted October 4, 2008 Author Share Posted October 4, 2008 Just use strtotime(). <?php $start = '1986-09-22'; $end = date('Y-m-d); $seconds = strtotime($end)-strtotime($start); ?> how do u get the age from seconds ?? Link to comment https://forums.phpfreaks.com/topic/126974-age-from-birthday/#findComment-656826 Share on other sites More sharing options...
F1Fan Posted October 4, 2008 Share Posted October 4, 2008 Hmm... Thinking more about this, you're wanting to say that if someone is 24 years and 11 months old, you want it to display 24 years old and not round up to 25, which makes sense. Try this: <?php function birthDate($bdate){ $btime = strtotime($bdate); $byear = date('Y',$btime);//birth year $bmonth = date('m',$btime);//birth month $bday = date('d',$btime);//birth day $nyear = date('Y');//this year $years = $nyear-$byear;//number of years since birth year if (strtotime("$nyear-$bmonth-$bday")>time()){ $years--;//subtract one year if they haven't had a birthday yet this year } return $years; } echo birthDate('1987-12-01'); ?> Link to comment https://forums.phpfreaks.com/topic/126974-age-from-birthday/#findComment-656848 Share on other sites More sharing options...
hmvrulz Posted October 4, 2008 Author Share Posted October 4, 2008 Hmm... Thinking more about this, you're wanting to say that if someone is 24 years and 11 months old, you want it to display 24 years old and not round up to 25, which makes sense. Try this: <?php function birthDate($bdate){ $btime = strtotime($bdate); $byear = date('Y',$btime);//birth year $bmonth = date('m',$btime);//birth month $bday = date('d',$btime);//birth day $nyear = date('Y');//this year $years = $nyear-$byear;//number of years since birth year if (strtotime("$nyear-$bmonth-$bday")>time()){ $years--;//subtract one year if they haven't had a birthday yet this year } return $years; } echo birthDate('1987-12-01'); ?> thaxn for the functions and your valuable time Link to comment https://forums.phpfreaks.com/topic/126974-age-from-birthday/#findComment-656864 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.