Dragen Posted April 17, 2007 Share Posted April 17, 2007 Hi, I'm trying to write a code to calculate my age. I've got a fully working script below: <?php $crntdate = date('d/m/Y'); $birth = "22/05/1988"; $crntsplit = explode("/", $crntdate); $birthsplit = explode("/", $birth); $crntdatestamp = mktime(0, 0, 0, $crntsplit[1], $crntsplit[0], $crntsplit[2]); $birthstamp = mktime(0, 0, 0, $birthsplit[1], $birthsplit[0], $birthsplit[2]); $seconds = $crntdatestamp - $birthstamp; $years = floor($seconds / 31536000); echo $years . " years"; ?> Just wondering if there was a better way of doing it as finding the seconds and dividing it all seems a bit of an odd way round.. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/47377-solved-calculate-age/ Share on other sites More sharing options...
Anzeo Posted April 17, 2007 Share Posted April 17, 2007 I don't know if my solution is better, but I'll post it right away. (I used my code from a tutorial and augmented it to use it for age calculations) Quote Link to comment https://forums.phpfreaks.com/topic/47377-solved-calculate-age/#findComment-231118 Share on other sites More sharing options...
Dragen Posted April 17, 2007 Author Share Posted April 17, 2007 ? where is it lol Quote Link to comment https://forums.phpfreaks.com/topic/47377-solved-calculate-age/#findComment-231126 Share on other sites More sharing options...
Dragen Posted April 17, 2007 Author Share Posted April 17, 2007 I've found a major problem with this method.. It works okay if I want to display my age, but I'm now trying to use a slightly modified version to say how long my site has been running for.. what I was wanting is something like this site has been running for about 1 year and 3 months the problem with my code is it can't define months. It will successfully tell me how many months between todays date and the start date, but not for examply 1 year and 3 months I end up with something ridiculous like 1 year and 43 months Quote Link to comment https://forums.phpfreaks.com/topic/47377-solved-calculate-age/#findComment-231165 Share on other sites More sharing options...
Anzeo Posted April 17, 2007 Share Posted April 17, 2007 Like I said I've got a code which display's the amount of days between two given date's <?php function DateDiff($dformat, $endDate, $beginDate) { $date_parts1=explode($dformat, $beginDate); $date_parts2=explode($dformat, $endDate); $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; } echo "".DateDiff("-",date("Y-m-d"),$RegDatum).""; ?> If you would divide this result by 365 you'd get an approximat age you could add another clause checking wether your birthdate has been passed or not. Quote Link to comment https://forums.phpfreaks.com/topic/47377-solved-calculate-age/#findComment-231352 Share on other sites More sharing options...
Dragen Posted April 17, 2007 Author Share Posted April 17, 2007 ah, thanks. I was trying to stay away from datediff though, because I heard it was unreliable.. Quote Link to comment https://forums.phpfreaks.com/topic/47377-solved-calculate-age/#findComment-231365 Share on other sites More sharing options...
Anzeo Posted April 17, 2007 Share Posted April 17, 2007 I don't think it's unreliable, as far as I've worked with it it seems to work fine. Only thing is, it's probably not the best method for age calculating. Quote Link to comment https://forums.phpfreaks.com/topic/47377-solved-calculate-age/#findComment-231382 Share on other sites More sharing options...
sasa Posted April 17, 2007 Share Posted April 17, 2007 your 1st code do noi work try <?php $crntdate = "18/05/2007"; $birth = "22/05/1988"; $crntsplit = explode("/", $crntdate); $birthsplit = explode("/", $birth); $crntdatestamp = mktime(0, 0, 0, $crntsplit[1], $crntsplit[0], $crntsplit[2]); $birthstamp = mktime(0, 0, 0, $birthsplit[1], $birthsplit[0], $birthsplit[2]); $seconds = $crntdatestamp - $birthstamp; $years = floor($seconds / 31536000); echo $years . " years"; ?> try <?php function my_old($birth, $curent = '') { $curent = $curent ? $curent : date('d/m/Y'); $birth = explode('/',$birth); $curent = explode('/', $curent); $days = array(31,31,28,31,30,31,30,31,31,30,31,30,31); if (($birth[2] % 4 == 0 and $birth[2] % 100 != 0) or $birth[2] % 400 == 0) $days[2] = 29; if ($birth[0] > $curent[0]) $curent[0] += $days[--$curent[1]]; if ($birth[1] > $curent[1]) { $curent[1] += 12; $curent[2] --; } return array('year' => $curent[2] - $birth[2], 'month' => $curent[1] - $birth[1], 'day' => $curent[0] - $birth[0]); } print_r(my_old('22/05/1988')); ?> Quote Link to comment https://forums.phpfreaks.com/topic/47377-solved-calculate-age/#findComment-231653 Share on other sites More sharing options...
Dragen Posted April 17, 2007 Author Share Posted April 17, 2007 it does work.. I'm using on my site at the moment. What do you think is wrong? Quote Link to comment https://forums.phpfreaks.com/topic/47377-solved-calculate-age/#findComment-231659 Share on other sites More sharing options...
sasa Posted April 17, 2007 Share Posted April 17, 2007 if today is $crntdate = "18/05/2007"; and i'm born $birth = "22/05/1988"; i'm not 19 years old Quote Link to comment https://forums.phpfreaks.com/topic/47377-solved-calculate-age/#findComment-231664 Share on other sites More sharing options...
Dragen Posted April 17, 2007 Author Share Posted April 17, 2007 here's my code: function birthdday($crntdate, $birth){ $crntsplit = explode("/", $crntdate); $birthsplit = explode("/", $birth); $crntdatestamp = mktime(0, 0, 0, $crntsplit[1], $crntsplit[0], $crntsplit[2]); $birthstamp = mktime(0, 0, 0, $birthsplit[1], $birthsplit[0], $birthsplit[2]); $seconds = $crntdatestamp - $birthstamp; $years = floor($seconds / 31536000); echo "<strong>Age:</strong> " . $years . " years"; } then I'm calling the function using: <?php birthdday(date('d/m/Y'), "22/05/1988"); ?> It says that I am 18. I'm not finding any problem with the age... Quote Link to comment https://forums.phpfreaks.com/topic/47377-solved-calculate-age/#findComment-231675 Share on other sites More sharing options...
sasa Posted April 17, 2007 Share Posted April 17, 2007 try <?php birthdday(date('d/m/Y'), "21/04/1988"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/47377-solved-calculate-age/#findComment-231685 Share on other sites More sharing options...
Dragen Posted April 17, 2007 Author Share Posted April 17, 2007 ah, it's not checking the days for some reason.. just months and years. Okay I understand now.. thanks! I'll have a look at your code when I have time... I'll let you know how it works for me. Quote Link to comment https://forums.phpfreaks.com/topic/47377-solved-calculate-age/#findComment-231690 Share on other sites More sharing options...
Dragen Posted April 20, 2007 Author Share Posted April 20, 2007 okay, I've finnally tested the code you gave me sasa. With a couple of modifications I've got it working perfectly to calculate both my age and how long my site's been running for! Thanks. here's my code: <?php function birthday($view, $birth, $curent = '') { $crnt = $crnt ? $crnt : date('d/m/Y'); $birth = explode('/',$birth); $crnt = explode('/', $crnt); $days = array(31,31,28,31,30,31,30,31,31,30,31,30,31); if (($birth[2] % 4 == 0 and $birth[2] % 100 != 0) or $birth[2] % 400 == 0) $days[2] = 29; if ($birth[0] > $crnt[0]) $crnt[0] += $days[--$crnt[1]]; if ($birth[1] > $crnt[1]) { $crnt[1] += 12; $crnt[2] --; } $age = array('year' => $crnt[2] - $birth[2], 'month' => $crnt[1] - $birth[1], 'day' => $crnt[0] - $birth[0]); if($view == age){ echo "<strong>Age:</strong> " . $age['year'] . " years"; }elseif($view == site){ echo $age['year'] . " "; if($age['year'] == 1){ echo "year "; }else{ echo "years "; } if($age['month'] > 0){ echo "and " . $age['month'] . " "; if($age['month'] == 1){ echo "month "; }else{ echo "months "; } } } } ?> If added some if statements to check if year and month is plural or not and also wether months should be included at all. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/47377-solved-calculate-age/#findComment-234167 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.