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?

Recommended Answers

All 2 Replies

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);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.