Skip to content

Instantly share code, notes, and snippets.

@hubsgz
Created March 11, 2014 02:24
Show Gist options
  • Save hubsgz/9478344 to your computer and use it in GitHub Desktop.
Save hubsgz/9478344 to your computer and use it in GitHub Desktop.
转换时间戳为n天/小时/分/秒前
<?php
/**
* 转换时间戳为n天/小时/分/秒前
*
* @param unknown $timestamp 时间戳
*
* @return string
*/
function convert2timeago($timestamp)
{
$nowtime = time();
$tmp = $nowtime - $timestamp;
$day_num = intval($tmp / (3600*24)); //天数
if ($day_num > 0) {
return $day_num . '天前';
}
$remain_hour = $tmp % (3600*24); //除去天数之后剩余秒数
$hour_num = intval($remain_hour/3600); //小时数
if ($hour_num>0) {
return $hour_num . '小时前';
}
$remain_minute = $remain_hour % 3600; //除去小时数之后剩余秒数
$minute_num = intval($remain_minute / 60); //分种数
if ($minute_num>0) {
return $minute_num . '分钟前';
}
$remain_second = $remain_minute % 60; //秒数
return $remain_second . '秒前';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment