kevinkhan Posted August 4, 2011 Share Posted August 4, 2011 i have the following date 1990-08-27 and i wanna find out how many days old the person is. Any ideas how to do it? Quote Link to comment https://forums.phpfreaks.com/topic/243780-date-of-birth-to-days-old-in-php/ Share on other sites More sharing options...
phpSensei Posted August 4, 2011 Share Posted August 4, 2011 You can do, not the best solution, but it works. return floor( (time() – strtotime($dateofbirth ) ) / 31556926); Also I tried to see what Google would return, you should try it more often http://stackoverflow.com/questions/3380990/php-calculate-persons-current-age Quote Link to comment https://forums.phpfreaks.com/topic/243780-date-of-birth-to-days-old-in-php/#findComment-1251694 Share on other sites More sharing options...
TeNDoLLA Posted August 4, 2011 Share Posted August 4, 2011 $bday = new DateTime('1980-11-27'); $today = new DateTime('now'); $diff = $bday->diff($today); echo $diff->format('%y years, %m months, %d days old.'); Quote Link to comment https://forums.phpfreaks.com/topic/243780-date-of-birth-to-days-old-in-php/#findComment-1251772 Share on other sites More sharing options...
kevinkhan Posted August 4, 2011 Author Share Posted August 4, 2011 $bday = new DateTime('1980-11-27'); $today = new DateTime('now'); $diff = $bday->diff($today); echo $diff->format('%y years, %m months, %d days old.'); im getting the following error when i run this Call to undefined method DateTime::diff() Quote Link to comment https://forums.phpfreaks.com/topic/243780-date-of-birth-to-days-old-in-php/#findComment-1251824 Share on other sites More sharing options...
TeNDoLLA Posted August 4, 2011 Share Posted August 4, 2011 You need (PHP 5 >= 5.3.0) for DateTime functions to work. Otherwise you will have to stick with phpSensei's method. Quote Link to comment https://forums.phpfreaks.com/topic/243780-date-of-birth-to-days-old-in-php/#findComment-1251825 Share on other sites More sharing options...
kevinkhan Posted August 4, 2011 Author Share Posted August 4, 2011 awh i only have 5.2.9 :( any other method of calculated number of days? and can you suggest any good hosting company's? Quote Link to comment https://forums.phpfreaks.com/topic/243780-date-of-birth-to-days-old-in-php/#findComment-1251829 Share on other sites More sharing options...
Muddy_Funster Posted August 4, 2011 Share Posted August 4, 2011 On the hosting company front I would recomend 34sp.com There is no way to easily compare dates in 5.2 that I know of, you would need to get/write a function to do it manualy. Quote Link to comment https://forums.phpfreaks.com/topic/243780-date-of-birth-to-days-old-in-php/#findComment-1251848 Share on other sites More sharing options...
TeNDoLLA Posted August 4, 2011 Share Posted August 4, 2011 Yeah and writing such a function will probably never be 100% accurate since there is not same amount of days in every year, not same amount of hours in every day (swaps between summer and winter time) etc. Either way writing the function would probably be huge pain in the ass. Quote Link to comment https://forums.phpfreaks.com/topic/243780-date-of-birth-to-days-old-in-php/#findComment-1251854 Share on other sites More sharing options...
ignace Posted August 4, 2011 Share Posted August 4, 2011 function calculate_age($birthday) { $time = strtotime($birthday); if ($time === FALSE || $time === -1) return FALSE; $a = idate('Y') - idate('Y', $time); $b = mktime(0, 0, 0, idate('n', $time), idate('j', $time), idate('Y')); if (time() < $b) --$a; return $a; } print calculate_age('1986-09-05').PHP_EOL; // 24 print calculate_age('1986-08-05').PHP_EOL; // 24 print calculate_age('1986-07-05').PHP_EOL; // 25 Just an idea. I prefer phpSensei's solution. Quote Link to comment https://forums.phpfreaks.com/topic/243780-date-of-birth-to-days-old-in-php/#findComment-1252010 Share on other sites More sharing options...
gizmola Posted August 4, 2011 Share Posted August 4, 2011 I am in the process of writing up a small article on how to do this directly with mysql SQL in a query. So if you have the birthdate in a mysql DATE type column, this query will get you a person's age, based on their birthdate. This query assumes a table named "bday", with the birthdate column named "bdate". You would need to adjust for your table and structure accordingly. You'd want to add an appropriate WHERE clause to get only one row as well. SELECT YEAR(CURDATE()) - YEAR(bdate) - IF(STR_TO_DATE(CONCAT(YEAR(CURDATE()), '-', MONTH(bdate), '-', DAY(bdate)) ,'%Y-%c-%e') >= CURDATE(), 1, 0) AS age FROM bday; Quote Link to comment https://forums.phpfreaks.com/topic/243780-date-of-birth-to-days-old-in-php/#findComment-1252037 Share on other sites More sharing options...
ignace Posted August 4, 2011 Share Posted August 4, 2011 SELECT YEAR(CURDATE()) - YEAR(bdate) - IF(STR_TO_DATE(CONCAT(YEAR(CURDATE()), '-', MONTH(bdate), '-', DAY(bdate)) ,'%Y-%c-%e') >= CURDATE(), 1, 0) AS age FROM bday; We think alike! Is it intended to calculate this for everyone in the bday table? Quote Link to comment https://forums.phpfreaks.com/topic/243780-date-of-birth-to-days-old-in-php/#findComment-1252069 Share on other sites More sharing options...
phpSensei Posted August 4, 2011 Share Posted August 4, 2011 Gizmola, Cool stuff! Quote Link to comment https://forums.phpfreaks.com/topic/243780-date-of-birth-to-days-old-in-php/#findComment-1252124 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.