Possible Duplicate:
How do I calculate relative time?
It shows 2 days,2 month,1 year and so on.
How to do it?
Possible Duplicate:
How do I calculate relative time?
It shows 2 days,2 month,1 year and so on.
How to do it?
I haven't seen the code base but I assume it would be something like (pseudocode since I know as much about PHP as I do about the mating habits of white rhinos):
string duration (n): // days
if n >= 365 return str(int(n/365)) + " years"
if n >= 30 return str(int(n/30)) + " months"
if n >= 7 return str(int(n/7)) + " weeks"
return str(int(n)) + " days"
Adjust the value passed in and the denominators if you want a finer resolution than a single day (and allow for "1 month"
instead of "1 months"
) and you're good to go.
I wouldn't worry too much about the inaccuracies of the division (e.g., the average month has about 30.44 days) since it's only supposed to be an approximation.
This is the PHP version of Pax pseudocode:
function duration ($n){
if ($n > 365) return ($n/365)." years";
if ($n > 30) return ($n/30)." months";
if ($n > 7) return ($n/7)." weeks";
return $n." days";
}