php函数将Unix时间戳转换为分钟、小时或天,如digg?
我需要一个php函数将unix时间戳转换为: 15秒 或 45分钟 或 3小时 而不是这样:2秒5分钟9小时 我只想要一个像digg这样的值(变得流行xx xxxxx)php函数将Unix时间戳转换为分钟、小时或天,如digg?,php,Php,我需要一个php函数将unix时间戳转换为: 15秒 或 45分钟 或 3小时 而不是这样:2秒5分钟9小时 我只想要一个像digg这样的值(变得流行xx xxxxx) 谢谢PHP没有内置函数来为您执行此操作,因此您必须手动执行。此函数将获取以天、小时、分钟或秒为单位的差值,并以“X天/小时/分钟/秒”格式返回: <?php function GetTimeDiff($timestamp) { $how_log_ago = ''; $seconds = time() - $
谢谢PHP没有内置函数来为您执行此操作,因此您必须手动执行。此函数将获取以天、小时、分钟或秒为单位的差值,并以“X天/小时/分钟/秒”格式返回:
<?php
function GetTimeDiff($timestamp) {
$how_log_ago = '';
$seconds = time() - $timestamp;
$minutes = (int)($seconds / 60);
$hours = (int)($minutes / 60);
$days = (int)($hours / 24);
if ($days >= 1) {
$how_log_ago = $days . ' day' . ($days != 1 ? 's' : '');
} else if ($hours >= 1) {
$how_log_ago = $hours . ' hour' . ($hours != 1 ? 's' : '');
} else if ($minutes >= 1) {
$how_log_ago = $minutes . ' minute' . ($minutes != 1 ? 's' : '');
} else {
$how_log_ago = $seconds . ' second' . ($seconds != 1 ? 's' : '');
}
return $how_log_ago;
?>
PHP没有内置函数来为您执行此操作,因此您必须手动执行。此函数将获取以天、小时、分钟或秒为单位的差值,并以“X天/小时/分钟/秒”格式返回:
<?php
function GetTimeDiff($timestamp) {
$how_log_ago = '';
$seconds = time() - $timestamp;
$minutes = (int)($seconds / 60);
$hours = (int)($minutes / 60);
$days = (int)($hours / 24);
if ($days >= 1) {
$how_log_ago = $days . ' day' . ($days != 1 ? 's' : '');
} else if ($hours >= 1) {
$how_log_ago = $hours . ' hour' . ($hours != 1 ? 's' : '');
} else if ($minutes >= 1) {
$how_log_ago = $minutes . ' minute' . ($minutes != 1 ? 's' : '');
} else {
$how_log_ago = $seconds . ' second' . ($seconds != 1 ? 's' : '');
}
return $how_log_ago;
?>
<代码> > P>如果这是一个网站,你可以考虑。否则,算法相当简单。比如:
$diff = $date - $now;
if ($diff > 2 * ONE_YEAR)
return sprintf("%d years", round($diff / ONE_YEAR));
else if ($diff > ONE_YEAR)
return "1 year";
else if ($diff > 2 * ONE_MONTH)
return sprintf("%d months", round($diff / ONE_MONTH));
...etc...
如果这是一个网站,你可以考虑。否则,算法相当简单。比如:
$diff = $date - $now;
if ($diff > 2 * ONE_YEAR)
return sprintf("%d years", round($diff / ONE_YEAR));
else if ($diff > ONE_YEAR)
return "1 year";
else if ($diff > 2 * ONE_MONTH)
return sprintf("%d months", round($diff / ONE_MONTH));
...etc...
试试这个:
function toFriendlyTime($seconds) {
$measures = array(
'day'=>24*60*60,
'hour'=>60*60,
'minute'=>60,
'second'=>1,
);
foreach ($measures as $label=>$amount) {
if ($seconds >= $amount) {
$howMany = floor($seconds / $amount);
return $howMany." ".$label.($howMany > 1 ? "s" : "");
}
}
return "now";
}
正如您所见,它还可以灵活地添加/删除您认为合适的时间度量。只需确保从最大到最小的顺序。测试:
print(
toFriendlyTime(0)."\n"
.toFriendlyTime(1)."\n"
.toFriendlyTime(2)."\n"
.toFriendlyTime(60)."\n"
.toFriendlyTime(3600)."\n"
.toFriendlyTime(24*3600)."\n"
);
结果:
now
1 second
2 seconds
1 minute
1 hour
1 day
试试这个:
function toFriendlyTime($seconds) {
$measures = array(
'day'=>24*60*60,
'hour'=>60*60,
'minute'=>60,
'second'=>1,
);
foreach ($measures as $label=>$amount) {
if ($seconds >= $amount) {
$howMany = floor($seconds / $amount);
return $howMany." ".$label.($howMany > 1 ? "s" : "");
}
}
return "now";
}
正如您所见,它还可以灵活地添加/删除您认为合适的时间度量。只需确保从最大到最小的顺序。测试:
print(
toFriendlyTime(0)."\n"
.toFriendlyTime(1)."\n"
.toFriendlyTime(2)."\n"
.toFriendlyTime(60)."\n"
.toFriendlyTime(3600)."\n"
.toFriendlyTime(24*3600)."\n"
);
结果:
now
1 second
2 seconds
1 minute
1 hour
1 day