cordoprod Posted April 29, 2008 Share Posted April 29, 2008 Hello! I have a timestamp made out of mktime() What i want is to calculate the age out of the timestamp. But i only want how many years.. for example: You are ... years old.. Understand? Thanks =) Quote Link to comment Share on other sites More sharing options...
zenag Posted April 29, 2008 Share Posted April 29, 2008 <?php $ageTime = mktime(0, 0, 0, 9, 9, 1919); $t = time(); $age = ($ageTime < 0) ? ( $t + ($ageTime * -1) ) : $t - $ageTime; $year = 60 * 60 * 24 * 365; $ageYears = $age / $year; echo 'You are ' . floor($ageYears) . ' years old.'; ?> Quote Link to comment Share on other sites More sharing options...
stylusrose Posted April 29, 2008 Share Posted April 29, 2008 I'm sure the above works fine, but I couldn't understand half of it. >_> anyway, this is the way I would have done it. <?php $birthTime = mktime(0,0,0,11,27,1987); $birthYear = strftime("%Y", $birthTime); //get's the birth year $birthMonth = strftime("%m", $birthTime); //get's birth month $currentYear = strftime( "%Y", time() ); //get's the current year $currentMonth = strftime( "%m", time() ); //get's current month $age = $currentYear - $birthYear; //your age if ($birthMonth > $currentMonth) $age--; //if your birth month hasn't past, you aren't a year older! echo "You are ". $age . "year's old!"; ?> Quote Link to comment Share on other sites More sharing options...
cordoprod Posted April 29, 2008 Author Share Posted April 29, 2008 Works now:) Thanks Quote Link to comment Share on other sites More sharing options...
haku Posted April 29, 2008 Share Posted April 29, 2008 This code will work, and will account for leap years as well. There may be a cleaner way of writing it, but it works. You will need to fill in the code for $old_timestamp, as it is the timestamp you referred to in your post. <?php $old_timestamp = _____; // This is the timestamp you referred to in your post. You need to fill in the code. $old_year = date("Y", $old_timestamp); $old_month = date("n", $old_timestamp); $new_timetamp = time(); $new_year = date("Y", $new_timetamp); $new_month = date("n", $new_timetamp); if($old_month != $new_month) { $age = ($old_month < $new_month) ? $new_year - $old_year : $new_year - $old_year - 1; } else { $old_day = date("j", $old_timestamp); $new_day = date("j", $new_timetamp); $age = ($old_day <= $new_day) ? $new_year - $old_year : $new_year - $old_year - 1; } echo ($age != 0) ? "age: {$age}" : "age: 0"; ?> Quote Link to comment Share on other sites More sharing options...
stylusrose Posted April 29, 2008 Share Posted April 29, 2008 oh my, lack of sleep is not good >_> you totally got me. for my code, you'd have to add in another line that gets the day using "%D"; and then an else if ((birthmonth == currentmonth) && (birthday < currentday)) >_> i think by now, I'd just use "zenag's" code, tho I really wish he added comments. Quote Link to comment Share on other sites More sharing options...
haku Posted April 29, 2008 Share Posted April 29, 2008 Zenag's code was good, and nice and compact. The only problem with it is that it will be off by a day for each leap year that occurs between the two dates. Quote Link to comment Share on other sites More sharing options...
stylusrose Posted April 29, 2008 Share Posted April 29, 2008 well.. he could put an if statement in for leap year, and instead of the 365, put 366... Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted April 29, 2008 Share Posted April 29, 2008 There is no need to do the month/day calculation separately. Here is a shorter version with some test data (uses yyyy-mm-dd as the DOB format, but can be changed to anything) - <?php // calculate age using php // (Current year - birth year) - 1 (if current mm-dd < birthday mm-dd) $dates = array(); $dates[] = "2008-01-16"; $dates[] = "2007-01-15"; $dates[] = "2007-01-16"; $dates[] = "2007-01-17"; $dates[] = "1900-01-15"; $dates[] = "1958-11-11"; $dates[] = "2006-01-16"; function year($date) { return substr($date, 0, 4); } // end of year function function mmdd($date) { return substr($date, 5,5); } // end of mmdd function function age($today,$date) { return year($today) - year($date) - (mmdd($today) < mmdd($date)); } // end of function age $today = date("Y-m-d"); foreach($dates as $date) { echo "DOB: $date, Age: " . age($today,$date) . "<br />"; } ?> Quote Link to comment 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.