954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Calculating Age

I am trying to figure out the best way to figure out "age" of a particular item. I currently have fields in a mySQL databases that are stored as datetime formats and I need to display (in PHP) the relative age of those dates in terms of years, months, and day. Any suggestions?

jrotunda85
Junior Poster
144 posts since Feb 2011
Reputation Points: 19
Solved Threads: 7
 
Shanti C
Posting Virtuoso
1,642 posts since Jul 2008
Reputation Points: 137
Solved Threads: 162
 

Try this;

$date1 = $rs['created_date'];//field from db
$date2 = date("Y-m-d");
										
$diff = abs(strtotime($date2) - strtotime($date1));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
										
printf("%d Years %d Months %d days\n",$years $months, $days);
Karthik_pranas
Posting Pro
564 posts since Feb 2011
Reputation Points: 96
Solved Threads: 97
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: