PHP: Calculate time difference in human readable way
function human_time_diff(int $seconds) {
$num = 0;
$unit = "";
if ($seconds < 300) {
$ret = "just now";
} elseif ($seconds < 3600) {
$num = intval($seconds / 60);
$unit = "minute";
} elseif ($seconds < 86400) {
$num = intval($seconds / 3600);
$unit = "hour";
} elseif ($seconds < 86400 * 30) {
$num = intval($seconds / 86400);
$unit = "day";
} elseif ($seconds < (86400 * 365)) {
$num = intval($seconds / (86400 * 30));
$unit = "month";
} else {
$num = intval($seconds / (86400 * 365));
$unit = "year";
}
if ($num > 1) {
$unit .= "s";
}
if ($unit) {
$ret = "$num $unit";
}
return $ret;
}
See also: Perl version