All articles(网络文学目录) All Pictures(图片目录) All Softwares(软件目录)

 
PHP多种格式显示日期函数_[PHP教程]

Writer: 归海一刀 Article type: Programming skills(编程技巧) Time: 2014/2/17 7:29:46 Browse times: 305 Comment times: 0

PHP多种格式显示日期函数_[PHP教程]


Head photo

Go homepage
Upload pictures
Write articles

PHP多种格式显示日期函数_[PHP教程] 显示效果如下:
2010-01-21 10:33:15
35分钟前
--------------------------------
2010-01-20 10:33:15
1天前
--------------------------------
2010-01-19 9:33:15
2天1小时前
--------------------------------
2010-01-14 10:33:15
1星期前
--------------------------------
2010-01-7 08:33:15
2星期前
--------------------------------
2009-12-13 10:33:15
1个月9天前
--------------------------------
2009-01-18 10:33:15
1年前
--------------------------------
2008-11-18 10:33:15
1年2月前
--------------------------------

代码:
function usertime($times)
{
if( $times == ’’ || $times==0) return false;
$dtime =is_int($times) ? $times : strtotime($times);
$ptime = time() - $dtime;
if( $ptime < 60 )
{
$pct = sprintf("%d秒前",$ptime);
}
else if( $ptime > 60 && $ptime < 3600 )
{
$pct = sprintf("%d分钟前",ceil( $ptime / 60 ));
}
else if( $ptime > 3600 && $ptime < (3600 * 24) )
{
$pct = sprintf("%d小时%d分钟前", floor( $ptime / 3600 ), ceil( ( $ptime % 3600 ) / 60 ));
}


else if( $ptime > (3600 * 24) && $ptime < (3600*24*7))
{
$d = $ptime / (3600*24);
$h = ( $ptime % (3600*24)) / 3600;
$m = ceil( ( ( $ptime % (3600*24)) % 3600 ) / 60 );
//$pct = sprintf("%d天%d小时%d分钟前", $d, $h, $m );
if($h>=1) {
$pct = sprintf("%d天%d小时前", $d, $h);
}
else{
$pct = sprintf("%d天前",$d);
}

}

else if( $ptime > (3600 * 24 *7) && $ptime < (3600*24*30))
{
$w = $ptime / (3600*24*7);
$d = ($ptime % (3600*24*7))/(3600*24);
$h=(($ptime%(3600*24*7))%(3600*24))/3600;
$m = ceil( ( ( $ptime % (3600*24)) % 3600 ) / 60 );
//$pct = sprintf("%d星期%d天%d小时前",$w, $d, $h);
if($d>=1) {
$pct = sprintf("%d星期%d天前",$w, $d);
}
else{
$pct = sprintf("%d星期前",$w);
}

}

else if( $ptime > (3600 * 24 *30) && $ptime < (3600*24*365))
{
$mt = $ptime / ( 3600*24*30);
$d = ($ptime % ( 3600*24 * 30))/ (3600*24);
$h = ( ($ptime % ( 3600*24 * 30))% (3600*24)) / 3600;
$m = ceil( ( ($ptime % ( 3600*24 * 30))% (3600*24)) % 3600 / 60 );
//$pct = sprintf("%d月%d天%d小时%d分钟前", $mt, $d, $h, $m );
if($d>=1) {
$pct = sprintf("%d个月%d天前",$mt,$d);
}
else{
$pct = sprintf("%d个月前",$mt);
}

}
else //n年前
{
$y = $ptime / (3600*24*365);
$m = ($ptime % (3600*24*30*12))/(3600*24*30);
if($m>=1) {
$pct = sprintf("%d年%d月前",$y,$m);
}
else{
$pct = sprintf("%d年前",$y);
}

}

return $pct;
}

?>




There are 0 records,
Comment:
Must be registered users to comment(必须是注册用户才能发表评论)

Disclaimer Privacy Policy About us Site Map
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.