Oct 27th

3

Relative time in PHP with flexible detail level

7 years 7 months ago comment 3 Comments

This post describes how you can display relative or nicer time values like 10 days, 4 hours and 6 minutes ago instead of a date like 26 October, 2009 6:45 PM. The function is flexible enough to take a parameter for the details level you desire – for example, in the above example, the detail level was 3. So, it displays days, hours and minutes. If it was 4, it would have displayed seconds too. If we were using an older date, detail level of 3 might have displayed years, months and weeks.

Here’s the code:

/*
 * Takes a unix timestamp and returns a relative time string such as "3 minutes ago",
 *   "2 months from now", "1 year ago", etc
 * The detailLevel parameter indicates the amount of detail. The examples above are
 * with detail level 1. With detail level 2, the output might be like "3 minutes 20
 *   seconds ago", "2 years 1 month from now", etc.
 * With detail level 3, the output might be like "5 hours 3 minutes 20 seconds ago",
 *   "2 years 1 month 2 weeks from now", etc.
 */
function nicetime($timestamp, $detailLevel = 1) {

	$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
	$lengths = array("60", "60", "24", "7", "4.35", "12", "10");

	$now = time();

	// check validity of date
	if(empty($timestamp)) {
		return "Unknown time";
	}

	// is it future date or past date
	if($now > $timestamp) {
		$difference = $now - $timestamp;
		$tense = "ago";

	} else {
		$difference = $timestamp - $now;
		$tense = "from now";
	}

	if ($difference == 0) {
		return "1 second ago";
	}

	$remainders = array();

	for($j = 0; $j < count($lengths); $j++) {
		$remainders[$j] = floor(fmod($difference, $lengths[$j]));
		$difference = floor($difference / $lengths[$j]);
	}

	$difference = round($difference);

	$remainders[] = $difference;

	$string = "";

	for ($i = count($remainders) - 1; $i >= 0; $i--) {
		if ($remainders[$i]) {
			$string .= $remainders[$i] . " " . $periods[$i];

			if($remainders[$i] != 1) {
				$string .= "s";
			}

			$string .= " ";

			$detailLevel--;

			if ($detailLevel <= 0) {
				break;
			}
		}
	}

	return $string . $tense;

}

And some examples:

echo nicetime(time());

The result would be “1 second ago”

echo nicetime(time() - 839283);

The result would be “1 week ago”

echo nicetime(time() - 8392783);

The result would be “3 months ago”

echo nicetime(time() - 8392783, 3);

The result would be “3 months 1 week 6 days ago”

echo nicetime(time() - 83592783, 3);

The result would be “2 years 7 months 2 weeks ago”

echo nicetime(time() - 83592783, 5);

The result would be “2 years 7 months 2 weeks 1 day 12 hours ago”

echo nicetime(time() + 90532, 5);

The result would be “1 day 1 hour 8 minutes 52 seconds from now”

3 Responses to “Relative time in PHP with flexible detail level”

  1. php develoepr says:

    I’m a little unsure on the accuracy of the function, it’s good for a basis but due to the varying amount of days in a month, 4.35 weeks is not accurate for calculating months. for example, this function would trip up on february.

    And that isn’t even counting leap years

    The rest is great but i feel the month’s part needs work

    A good basis though

    • Yes, that part isn’t 100% accurate.
      As you mentioned, getting it to work in all cases will require a lot of code.
      Considering that the function is used to take an exact time and make it an approximate time, I think that might be unnecessary extra work for most use cases.

      If anyone has the time to correct this part of the code, please do share your code :)

      Ankur

  2. Levi says:

    I like this code!

    I don’t think its a big problem that its not 100% accurate, if you are fetching users with it, nobody would care about it…
    For me, it was a perfect start, just what i was looking for! If sometime i would need better accuration, i will integrate it, and send the code for you.

    Thanks a lot!

Leave a Reply