paultfh Posted July 25, 2006 Share Posted July 25, 2006 How do I take a datetime type mysql and calculate how many days old it is?Example:$res = mysql_query("SELECT date FROM groups WHERE ID = {$arr['groupID']}");$date = mysql_result($res2,0,0);//here I want to calculate how many days old $date isThanks Quote Link to comment Share on other sites More sharing options...
fenway Posted July 26, 2006 Share Posted July 26, 2006 Days old? You mean how many days have elapsed between NOW() and the date? Try using the TO_DAYS() function. Quote Link to comment Share on other sites More sharing options...
paultfh Posted July 26, 2006 Author Share Posted July 26, 2006 I figured it out.My code:[quote] //age calculation $date1 = strtotime($date); $datenow = time(); $diff = $datenow - $date1; $diff /= 86400; //if age is less than 1 day output in hours else output in days if($diff < 1) { $diff *= 24; $age = round($diff,1); echo "<td width=\"85%\">".$age." hour(s)"; }else{ $age = round($diff,1); echo "<td width=\"85%\">".$age." day(s)"; } [/quote] Quote Link to comment Share on other sites More sharing options...
fenway Posted July 26, 2006 Share Posted July 26, 2006 That's one way... of course, the DB can do this for you as well without call so many PHP functions, but it's up to you. 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.