Php 天、小时、分钟、秒的时间戳
如何更改此函数,使其单独打印分钟单位 我的意思是: 现在是Php 天、小时、分钟、秒的时间戳,php,Php,如何更改此函数,使其单独打印分钟单位 我的意思是: 现在是 This was 7 second ago -- Couple minutes later -- This was 5 minute 8 second ago 但我想要这个: This was 7 second ago -- Couple minutes later -- This was 5 minute ago ( i dont care about the seconds ) 还有,我如何检查它的复数形式?那么它会在单位后面加
This was 7 second ago
-- Couple minutes later --
This was 5 minute 8 second ago
但我想要这个:
This was 7 second ago
-- Couple minutes later --
This was 5 minute ago ( i dont care about the seconds )
还有,我如何检查它的复数形式?那么它会在单位后面加一个S
职能:
function humanTiming($time)
{
$time = time() - $time; // to get the time since that moment
$tokens = array (
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second'
);
$result = '';
$counter = 1;
foreach ($tokens as $unit => $text) {
if ($time < $unit) continue;
if ($counter > 2) break;
$numberOfUnits = floor($time / $unit);
$result .= "$numberOfUnits $text ";
$time -= $numberOfUnits * $unit;
++$counter;
}
return "This was {$result} ago";
}
功能定时($time)
{
$time=time()-$time;//获取从该时刻开始的时间
$tokens=数组(
31536000=>“年”,
2592000=>“月”,
604800=>“一周”,
86400=>“天”,
3600=>“小时”,
60=>“分钟”,
1=>“秒”
);
$result='';
$counter=1;
foreach($unit=>$text形式的令牌){
如果($时间<$单位)继续;
如果($counter>2)中断;
$numberOfUnits=楼层($time/$unit);
$result.=“$numberOfUnits$text”;
$time-=$numberOfUnits*$unit;
++$counter;
}
返回“这是{$result}年前”;
}
检查PHP类,您应该使用它,而不是手动操作。替换它
$numberOfUnits = floor($time / $unit);
用这个
$numberOfUnits = floor($time / $unit);
If ( (int) $numberOfUnits > 1 )
{
$text .= 's';
}
这可能是复数形式的解决方案这里有一种使用类(函数取自)的方法: 示例:
echo human_timing(time() - 20);
echo human_timing(time() - 1000);
echo human_timing(time() - 5500);
输出:
20秒前
16分钟前
一小时前
echo human_timing(time() - 20);
echo human_timing(time() - 1000);
echo human_timing(time() - 5500);