我写了一个简单的方法,它从日期开始返回经过了多少天的小时和秒。
例如,它返回 15000 天 3 小时 3 秒前……在用户端似乎不太好阅读。
我怎样才能做得更好?
例如像facebook发布时间之类的东西?
任何人都可以照顾这段代码吗?
public function date_diff($start, $end="NOW")
{
$timeshift = false;
$sdate = strtotime($start);
$edate = strtotime($end);
$time = $edate - $sdate;
if($time >= 0 && $time <= 59) {
// Seconds
if($time == 1)
{
$s = 'second';
}
else
{
$s = 'seconds';
}
$timeshift = $time." ".$s;
} elseif($time>=60 && $time<=3599) {
// Minutes + Seconds
$pmin = ($edate - $sdate) / 60;
$premin = explode('.', $pmin);
if($premin[0] == 1)
{
$m = 'min';
}
else
{
$m = 'min';
}
// $presec = $pmin-$premin[0];
//$sec = $presec*60;
$timeshift = $premin[0]." ".$m;//.round($sec,0); // sec ';
} elseif($time>=3600 && $time<=86399) {
// Hours + Minutes
$phour = ($edate - $sdate) / 3600;
$prehour = explode('.',$phour);
$premin = $phour-$prehour[0];
$min = explode('.',$premin*60);
if($prehour[0] > 1)
{
$h = 'hours';
}
else
{
$h = 'hour';
}
if($min[0] == 1)
{
$m = 'min';
}
else
{
$m = 'min';
}
// $presec = '0.'.$min[1];
// $sec = $presec*60;
$timeshift = $prehour[0]." ".$h." ".$min[0]." ".$m;//.round($sec,0).' sec ';
} elseif($time>=86400) {
// Days + Hours + Minutes
$pday = ($edate - $sdate) / 86400;
$preday = explode('.',$pday);
$phour = $pday-$preday[0];
$prehour = explode('.',$phour*24);
$premin = ($phour*24)-$prehour[0];
$min = explode('.',$premin*60);
// $presec = '0.'.$min[1];
// $sec = $presec*60;
if($preday[0] > 1)
{
$d = 'days';
}
else
{
$d = 'day';
}
if($prehour[0] > 1)
{
$h = 'hours';
}
else
{
$h = 'hour';
}
if($min[0] == 1)
{
$m = 'min';
}
else
{
$m = 'min';
}
$timeshift = $preday[0]." ".$d." ".$prehour[0]." ".$h." ".$min[0]." ".$m;
}
return $timeshift.' ago';
}
thx 并希望有人会喜欢这段代码