marksie1988 Posted August 7, 2007 Share Posted August 7, 2007 i have the flowing table CREATE TABLE `band` ( `id` int(10) unsigned NOT NULL auto_increment, `name` varchar(50) NOT NULL, `pos` varchar(50) NOT NULL, `dob` date NOT NULL default '0001-01-01', `bio` text NOT NULL, `equip` text NOT NULL, `songw` text NOT NULL, `inf` text NOT NULL, `goals` text NOT NULL, `myspace` carchar(200) NOT NULL, `image` text NOT NULL, PRIMARY KEY (`id`) ) and i also have a page that displays the information but i want to display the dob (date of birth) column as the age of a person how do i do this? currently all i can get it to do is show todays date or the date from the table but i cant get it to show an age. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/63737-solved-date-of-birth-as-age/ Share on other sites More sharing options...
dbo Posted August 7, 2007 Share Posted August 7, 2007 You're right in not putting age in your database. Age is a derivable field based off of date of birth. You'll just need to write a function that does the conversion for you. Quote Link to comment https://forums.phpfreaks.com/topic/63737-solved-date-of-birth-as-age/#findComment-317578 Share on other sites More sharing options...
drewbee Posted August 7, 2007 Share Posted August 7, 2007 Here is the basic concept: <? $dateOfBirth = "1980-02-01"; // Split the date of birth into an array $dateOfBirth = explode('-', $dateOfBirth); // create a timestamp of the users date of birth $dobTimestamp = mktime(0,0,0,$dateOfBirth['1'], $dateOfBirth['2'], $dateOfBirth['0']); // Subtract date of birth timestamp from the current time $ageInSeconds = $dobTimestamp - time(); // you now have the number of seconds the person has been alive for. // Simply do the math to change it to days, years etc. ?> Quote Link to comment https://forums.phpfreaks.com/topic/63737-solved-date-of-birth-as-age/#findComment-317588 Share on other sites More sharing options...
marksie1988 Posted August 7, 2007 Author Share Posted August 7, 2007 this returns an age of minus look -594834538 is what was returned? fixed with this $ageInSeconds = time() - $dobTimestamp; Quote Link to comment https://forums.phpfreaks.com/topic/63737-solved-date-of-birth-as-age/#findComment-317604 Share on other sites More sharing options...
drewbee Posted August 7, 2007 Share Posted August 7, 2007 Ooops. Nice catch. I typed that up real quick so their may be bugs with it, nothing was validated. That is correct though, you have to subtract the current time from the previous time (I put it backwards). Quote Link to comment https://forums.phpfreaks.com/topic/63737-solved-date-of-birth-as-age/#findComment-317608 Share on other sites More sharing options...
marksie1988 Posted August 7, 2007 Author Share Posted August 7, 2007 sorted it out now thanks for the help here is the code finished $dateOfBirth = $row['dob']; // Split the date of birth into an array $dateOfBirth = explode('-', $dateOfBirth); // create a timestamp of the users date of birth $dobTimestamp = mktime(0,0,0,$dateOfBirth['1'], $dateOfBirth['2'], $dateOfBirth['0']); // Subtract date of birth timestamp from the current time $ageinseconds = time() - $dobTimestamp; $days = $ageinseconds / 86400; $years = floor($days / 365); $days = $days % 365; then you just echo $years for the number of years and $days for the number of days Quote Link to comment https://forums.phpfreaks.com/topic/63737-solved-date-of-birth-as-age/#findComment-317621 Share on other sites More sharing options...
AndyB Posted August 7, 2007 Share Posted August 7, 2007 I don't want to rain on your parade, but ... every fourth year has 366 days. Quote Link to comment https://forums.phpfreaks.com/topic/63737-solved-date-of-birth-as-age/#findComment-317661 Share on other sites More sharing options...
drewbee Posted August 7, 2007 Share Posted August 7, 2007 Ohh Andy, always with the raining!! <?php $dateOfBirth = $row['dob']; // Split the date of birth into an array $dateOfBirth = explode('-', $dateOfBirth); // create a timestamp of the users date of birth $dobTimestamp = mktime(0,0,0,$dateOfBirth['1'], $dateOfBirth['2'], $dateOfBirth['0']); // Subtract date of birth timestamp from the current time $ageinseconds = time() - $dobTimestamp; $days = $ageinseconds / 86400; $years = floor($days / 365); $days = $days % 365; $leapDays = floor($years / 4); $days = $days + $leapDays; ?> is this correct? Makes sense to me but I've been working to much today and my mind is cooked! I just realized this isn't correct. The day addition doesn't funnel itself down through the years/months etc so the days have to be added before any other calcuation is made. Quote Link to comment https://forums.phpfreaks.com/topic/63737-solved-date-of-birth-as-age/#findComment-317679 Share on other sites More sharing options...
drewbee Posted August 7, 2007 Share Posted August 7, 2007 Is it just me or is the calender the most organized, illogical item on this planet? Quote Link to comment https://forums.phpfreaks.com/topic/63737-solved-date-of-birth-as-age/#findComment-317681 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.