Advertisements

human_time_diff

Definition:
function human_time_diff( $from, $to = '' ) {}

Determines the difference between two timestamps.
The difference is returned in a human readable format such as "1 hour", "5 mins", "2 days".

Parameters

  • int $from: Unix timestamp from which the difference begins.
  • int $to: Optional. Unix timestamp to end the time difference. Default becomes time() if not set.

Return values

returns:Human readable time difference.

Source code

function human_time_diff( $from, $to = '' ) {

	if ( empty($to) )

		$to = time();

	$diff = (int) abs($to - $from);

	if ($diff <= 3600) {

		$mins = round($diff / 60);

		if ($mins <= 1) {

			$mins = 1;

		}

		/* translators: min=minute */

		$since = sprintf(_n('%s min', '%s mins', $mins), $mins);

	} else if (($diff <= 86400) && ($diff > 3600)) {

		$hours = round($diff / 3600);

		if ($hours <= 1) {

			$hours = 1;

		}

		$since = sprintf(_n('%s hour', '%s hours', $hours), $hours);

	} elseif ($diff >= 86400) {

		$days = round($diff / 86400);

		if ($days <= 1) {

			$days = 1;

		}

		$since = sprintf(_n('%s day', '%s days', $days), $days);

	}

	return $since;

}

1959

Advertisements

Trackbacks/Pingbacks

  1. timestamp - SitePoint Forums - July 12, 2011

    […] Human Readable Form (Facebook Style) – Tech Thought WordPress uses the human_time_diff() function: human_time_diff | A HitchHackers guide through WordPress __________________ 301tool 1.1.3 – URL redirector & shortener (PHP/MySQL) Can be hosted on […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: