Skip to content

Instantly share code, notes, and snippets.

@kayue
Created January 10, 2011 04:13
Show Gist options
  • Save kayue/772343 to your computer and use it in GitHub Desktop.
Save kayue/772343 to your computer and use it in GitHub Desktop.
Get relative time in WordPress
<?php
// This function will return "now", "3 seconds ago", "1 month ago" etc ...
function get_the_relative_time($time = null)
{
if(is_null($time)) $time = get_the_time("U");
$time_diff = date("U") - $time; // difference in second
$second = 1;
$minute = 60;
$hour = 60*60;
$day = 60*60*24;
$week = 60*60*24*7;
$month = 60*60*24*7*30;
$year = 60*60*24*7*30*365;
if ($time_diff <= 120) {
$output = "now";
} elseif ($time_diff > $second && $time_diff < $minute) {
$output = round($time_diff/$second)." second";
} elseif ($time_diff >= $minute && $time_diff < $hour) {
$output = round($time_diff/$minute)." minute";
} elseif ($time_diff >= $hour && $time_diff < $day) {
$output = round($time_diff/$hour)." hour";
} elseif ($time_diff >= $day && $time_diff < $week) {
$output = round($time_diff/$day)." day";
} elseif ($time_diff >= $week && $time_diff < $month) {
$output = round($time_diff/$week)." week";
} elseif ($time_diff >= $month && $time_diff < $year) {
$output = round($time_diff/$month)." month";
} elseif ($time_diff >= $year && $time_diff < $year*10) {
$output = round($time_diff/$year)." year";
} else{ $output = " more than a decade ago"; }
if ($output <> "now") {
$output = (substr($output,0,2)<>"1 ") ? $output."s" : $output;
$output .= " ago";
}
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment