Created
March 13, 2019 06:15
-
-
Save Jason-cqtan/03d4838168f1441874991122ca5deefe to your computer and use it in GitHub Desktop.
时间戳转换成“刚刚”,“5分钟前”,“3小时前”等
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 | |
date_default_timezone_set('PRC'); | |
/** | |
* [时间戳转换成“刚刚”,“5分钟前”,“3小时前”等] | |
* @param [string] $time [时间戳] | |
* @return [string] | |
*/ | |
function transfer_time($time) | |
{ | |
$original_time = $time; | |
$rtime = date("m-d H:i",$time); | |
$htime = date("H:i",$time); | |
$current_time = time(); | |
$time = $current_time - $time; | |
if ($time < 60) | |
{ | |
$str = '刚刚'; | |
} | |
elseif ($time < 60 * 60) | |
{ | |
$min = floor($time/60); | |
$str = $min.'分钟前'; | |
} | |
elseif ($time < 60 * 60 * 24) | |
{ | |
$h = floor($time/(60*60)); | |
$str = $h.'小时前 '.$htime; | |
} | |
elseif ($time < 60 * 60 * 24 * 3) | |
{ | |
$d = floor($time/(60*60*24)); | |
if($d==1) | |
$str = '昨天 '.$htime; | |
else | |
$str = '前天 '.$htime; | |
} | |
else | |
{ | |
$current_year = (int)date("Y",$current_time); | |
$original_year = (int)date("Y",$original_time); | |
if($current_year > $original_year){ | |
$str = date("Y-m-d H:i",$original_time); | |
}else{ | |
$str = $rtime; | |
} | |
} | |
return $str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment