投稿から日付を取得し、投稿された時間を書き込む機能があります。関数を使用すると、奇妙な理由で 43 年と 4 か月前に投稿されたと表示されます。
function relativeTime($date,$precision=2)
{
$times=array(365*24*60*60 => "year",
30*24*60*60 => "month",
7*24*60*60 => "week",
24*60*60 => "day",
60*60 => "hour",
60 => "minute",
1 => "second");
$passed=time()-$date;
if($passed<5)
{
$output='less than 5 seconds ago';
}
else
{
$output=array();
$exit=0;
foreach($times as $period=>$name)
{
if($exit>=$precision || ($exit>0 && $period<60)) break;
$result = floor($passed/$period);
if($result>0)
{
$output[]=$result.' '.$name.($result==1?'':'s');
$passed-=$result*$period;
$exit++;
}
else if($exit>0) $exit++;
}
$output=implode(' and ',$output).' ago';
}
return $output;
}
次を使用して日付を入力しています:
$date = date("Y-m-d H:i:s");
$passed=time()-$date
オフですか?
みんな、ありがとう!