Last active
August 29, 2015 14:00
-
-
Save qubaomingg/11361274 to your computer and use it in GitHub Desktop.
时间格式转换小工具函数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Util_time{ | |
public function __coustruct(){} | |
/** | |
* 计算两个时间戳相隔多少天 小时 分钟 秒 传入秒 | |
*/ | |
public function timediff($begin_time,$end_time) { | |
if($begin_time < $end_time){ | |
$starttime = $begin_time; | |
$endtime = $end_time; | |
} | |
else{ | |
$starttime = $end_time; | |
$endtime = $begin_time; | |
} | |
$timediff = $endtime-$starttime; | |
$days = intval($timediff/86400); | |
$remain = $timediff%86400; | |
$hours = intval($remain/3600); | |
$remain = $remain%3600; | |
$mins = intval($remain/60); | |
$secs = $remain%60; | |
$res = array("day" => $days,"hour" => $hours,"min" => $mins,"sec" => $secs); | |
return $res; | |
} | |
public function count_time($time) { | |
$time = $this->timediff(strtotime($time), time()); | |
$last_time = ''; | |
if($time['day'] != 0 ) { | |
$last_time = $time['day'].'天'; | |
} | |
if($time['hour'] != 0 ) { | |
$last_time = $last_time.$time['hour'].'小时'; | |
} | |
if($time['min'] != 0 ) { | |
$last_time = $last_time.$time['min'].'分钟'; | |
} | |
if($time['sec'] != 0 ) { | |
$last_time = $last_time.$time['sec'].'秒'; | |
} | |
return $last_time; | |
} | |
/** | |
* 调整时间为XX分钟前 | |
*/ | |
public function date_ajust($time) { | |
$time_diff = $this->timediff(time(), strtotime($time)); | |
$out_time = ''; | |
if( $time_diff['day'] == 0 && $time_diff['hour'] == 0) { | |
$out_time= $time_diff['min'].'分钟前'; | |
} else if ( $time_diff['day'] == 0 && $time_diff['hour'] != 0) { | |
$out_time = '今天'.date('H:i', strtotime($time)); | |
} else { | |
$out_time = date('m-d H:i', strtotime($time)); | |
} | |
return $out_time; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment