0

Possible Duplicate:
How do I calculate relative time?

It shows 2 days,2 month,1 year and so on.

How to do it?

Community
  • 1
  • 1
Misier
  • 1,455
  • 4
  • 13
  • 15
  • 2
    Originally asked a (relatively) long time ago: http://stackoverflow.com/questions/11/how-do-i-calculate-relative-time ;-) – Matt Hamilton Oct 06 '09 at 11:17

2 Answers2

2

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
-1

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";
}
jerjer
  • 8,694
  • 30
  • 36