php自定義函數(shù)計(jì)算剩余時(shí)間,把兩個(gè)日期格式的字符串轉(zhuǎn)化成unix時(shí)間戳,然后相減獲得時(shí)間戳差。最后判斷剩余時(shí)間,生成類(lèi)似(2小時(shí)30分鐘20秒前發(fā)布)這樣的時(shí)間格式.
public function gettime($time_s,$time_n){
$time_s = strtotime($time_s);
$time_n = strtotime($time_n);
$strtime = '';
$time = $time_n-$time_s;
if($time >= 86400){
return $strtime = date('Y-m-d H:i:s',$time_s);
}
if($time >= 3600){
$strtime .= intval($time/3600).'小時(shí)';
$time = $time % 3600;
}else{
$strtime .= '';
}
if($time >= 60){
$strtime .= intval($time/60).'分鐘';
$time = $time % 60;
}else{
$strtime .= '';
}
if($time > 0){
$strtime .= intval($time).'秒前';
}else{
$strtime = "時(shí)間錯(cuò)誤";
}
return $strtime;
}
更多信息請(qǐng)查看IT技術(shù)專(zhuān)欄