function RelativeTime($timestamp) {
$difference = time() - $timestamp;
$periods = array(
"sec", "min", "hour", "day", "week", "month", "years", "decade"
);
$lengths = array("60", "60", "24", "7", "4.35", "12", "10");
if ($difference > 0) { // this was in the past
$ending = "ago";
} else { // this was in the future
$difference = -$difference;
$ending = "to go";
}
for ($j = 0; $difference >= $lengths[$j]; $j++)
$difference /= $lengths[$j];
$difference = round($difference);
if ($difference != 1) $periods[$j] .= "s";
$text = "$difference $periods[$j] $ending";
return $text;
}
上記のPHP関数をインターウェブで見つけました。はるか先の日付に問題があることを除けば、かなりうまく機能しているようです。
たとえば、ループするPHPエラーが発生します
ゼロ除算
$difference /= $lengths[$j];
日付が2033年の場合。
それを修正する方法はありますか?アレイはすでに数十年を占めているので、2033年が「あと20年」のようなものになることを期待しています。