<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ankur&#039;s PHP and Javascript blog &#187; Relaive time</title>
	<atom:link href="http://www.ankur.com/blog/tag/relaive-time/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ankur.com/blog</link>
	<description>PHP and javascript blog</description>
	<lastBuildDate>Tue, 10 Nov 2009 14:28:20 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Relative time in PHP with flexible detail level</title>
		<link>http://www.ankur.com/blog/100/php/relative-time-php-flexible-detail-level/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=relative-time-php-flexible-detail-level</link>
		<comments>http://www.ankur.com/blog/100/php/relative-time-php-flexible-detail-level/#comments</comments>
		<pubDate>Tue, 27 Oct 2009 18:36:10 +0000</pubDate>
		<dc:creator>Ankur Motreja</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Relaive time]]></category>

		<guid isPermaLink="false">http://www.ankur.com/blog/?p=100</guid>
		<description><![CDATA[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 &#8211; for example, in the above example, the detail [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>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 &#8211; 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.</p>
<p><span id="more-100"></span></p>
<p>Here&#8217;s the code:</p>
<pre class="brush: php;">
/*
 * Takes a unix timestamp and returns a relative time string such as &quot;3 minutes ago&quot;,
 *   &quot;2 months from now&quot;, &quot;1 year ago&quot;, 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 &quot;3 minutes 20
 *   seconds ago&quot;, &quot;2 years 1 month from now&quot;, etc.
 * With detail level 3, the output might be like &quot;5 hours 3 minutes 20 seconds ago&quot;,
 *   &quot;2 years 1 month 2 weeks from now&quot;, etc.
 */
function nicetime($timestamp, $detailLevel = 1) {

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

	$now = time();

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

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

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

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

	$remainders = array();

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

	$difference = round($difference);

	$remainders[] = $difference;

	$string = &quot;&quot;;

	for ($i = count($remainders) - 1; $i &gt;= 0; $i--) {
		if ($remainders[$i]) {
			$string .= $remainders[$i] . &quot; &quot; . $periods[$i];

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

			$string .= &quot; &quot;;

			$detailLevel--;

			if ($detailLevel &lt;= 0) {
				break;
			}
		}
	}

	return $string . $tense;

}
</pre>
<p>And some examples:</p>
<pre class="brush: php;">
echo nicetime(time());
</pre>
<p>The result would be &#8220;1 second ago&#8221;</p>
<pre class="brush: php;">
echo nicetime(time() - 839283);
</pre>
<p>The result would be &#8220;1 week ago&#8221;</p>
<pre class="brush: php;">
echo nicetime(time() - 8392783);
</pre>
<p>The result would be &#8220;3 months ago&#8221;</p>
<pre class="brush: php;">
echo nicetime(time() - 8392783, 3);
</pre>
<p>The result would be &#8220;3 months 1 week 6 days ago&#8221;</p>
<pre class="brush: php;">
echo nicetime(time() - 83592783, 3);
</pre>
<p>The result would be &#8220;2 years 7 months 2 weeks ago&#8221;</p>
<pre class="brush: php;">
echo nicetime(time() - 83592783, 5);
</pre>
<p>The result would be &#8220;2 years 7 months 2 weeks 1 day 12 hours ago&#8221;</p>
<pre class="brush: php;">
echo nicetime(time() + 90532, 5);
</pre>
<p>The result would be &#8220;1 day 1 hour 8 minutes 52 seconds from now&#8221;</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.ankur.com/blog/100/php/relative-time-php-flexible-detail-level/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
