Skip to content

Instantly share code, notes, and snippets.

@Jason-cqtan
Created March 13, 2019 06:15
Show Gist options
  • Save Jason-cqtan/03d4838168f1441874991122ca5deefe to your computer and use it in GitHub Desktop.
Save Jason-cqtan/03d4838168f1441874991122ca5deefe to your computer and use it in GitHub Desktop.
时间戳转换成“刚刚”,“5分钟前”,“3小时前”等
<?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