-
-
Save idev247/928914 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// This function will return "now", "3 seconds ago", "1 month ago" etc ... | |
// example: echo 'posted ' . get_the_relative_time(mktime(date("H"), date("i"), date("s"), date("m")-1, date("d"), date("Y"))); | |
function get_the_relative_time($time = null, $now = 20) { | |
if(is_null($time)) $time = date('U'); | |
$time_diff = date("U") - $time; // difference in seconds | |
$second = 1; | |
$minute = 60; | |
$hour = 60*60; | |
$day = 60*60*24; | |
$week = 60*60*24*7; | |
$month = 60*60*24*30; | |
$year = 60*60*24*365; | |
$decade = false; | |
if ($time_diff <= $now) { | |
$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{ $decade = true; $output = " more than a decade ago"; } | |
if ($output != "now" && !$decade) { | |
$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