如果我的代码如下:
if($seconds < 60)
$interval = "$seconds seconds ago";
else
if($seconds < 3600)
$interval = floor($seconds / 60) . "minutes ago";
else
if($seconds < 86400)
$interval = floor($seconds / 3600) . "hours ago";
else
$interval = floor($seconds / 86400) . "days ago";
我怎么能摆脱它说的话:
1天前。 1年前 1分钟前。 1小时前。
谢谢:)
答案 0 :(得分:7)
可以用三元运算符非常简洁地完成:
if($seconds < 60) {
$interval = "$seconds second" . (($seconds != 1) ? "s" : "") . " ago";
} else {
if($seconds < 3600) {
$minutes = floor($seconds / 60);
$interval = "$minutes minute" . (($minutes > 1) ? "s" : "") . " ago";
} else {
if($seconds < 86400) {
$hours = floor($seconds / 3600);
$interval = "$hours hour" . (($hours > 1) ? "s" : "") . " ago";
} else {
$days = floor($seconds / 86400);
$interval = "$days day" . (($days > 1) ? "s" : "") . " ago";
}
}
}
答案 1 :(得分:7)
如果您的应用是国际性的并使用gettext扩展程序,则可以执行以下操作:
sprintf(ngettext('%d minute', '%d minutes', $amount), $amount);
您可以为它创建一个包装函数:
function pluralize($singular, $plural, $num) {
return sprintf(ngettext($singular, $plural, $num), $num);
}
这是最好的方式。
答案 2 :(得分:3)
又一种解决方案。
if($seconds < 60)
$interval = "$seconds second";
else
if($seconds < 3600)
$interval = floor($seconds / 60) . " minute";
else
if($seconds < 86400)
$interval = floor($seconds / 3600) . " hour";
else
$interval = floor($seconds / 86400) . " day";
$interval .= (reset(explode(" ", $interval)) != 1 ? "s" : "")." ago";
答案 3 :(得分:2)
$time = "120";
$array = array("second" => 1,
"minute" => 60,
"hour" => 60,
"day" => 24,
"year" => 365);
$old_time = 0;
$old_type = false;
// Loop through each type
foreach($array as $type => $seconds)
{
// Divide
$time = floor($time/$seconds);
// If it went into a value lower than 0, stop dividing
if($time < 1)
{
$time = $old_time;
$type = $old_type;
break;
}
else
{
// Continue dividing.
$old_time = $time;
$old_type = $type;
}
}
if($time == 1)
{
$interval = $time . " ". $type . " ago";
}
else
{
$interval = $time ." " . $type . "s ago";
}
echo $interval;
这将划分所有可能的时间类型,并给出一个不会将其转换为分数的时间类型。通过将数值与类型分开,我们可以测试数字是否= = 1,并更正单词。
答案 4 :(得分:1)
if ($interval < 60) {
$unit = 'Second';
} else if ($interval < 1440) {
$unit = 'Minute'; $interval /= 60;
} else if ($interval < 86400) {
$unit = 'Hour'; $interval /= 1440;
} else {
$unit = 'Day'; $interval /= 86400;
}
$interval = intval($interval);
$interval = "$interval $unit" . ($interval == 1 ? '' : 's') . " ago";
答案 5 :(得分:0)
这是一个用于获取“1个月,2周和30秒前”字符串的通用PHP片段。
$timeUnits = array("month" => 2592000, "week" => 604800, "day" => 86400, "hour" => 3600, "minute" => 60, "second" => 1); $tmpSeconds = $seconds; $timeAgoStrings = array(); foreach ($timeUnits as $name => $numOfSeconds) { if ($seconds > $numOfSeconds) { $val = floor($tmpSeconds / $numOfSeconds); $agoStr = ($val > 1) ? $name."s" : $name; $timeAgoStrings[] = "$val $agoStr"; $tmpSeconds = $tmpSeconds - $val; // cut the used time units off our tmpSeconds variable } } //check if we have more than one string - in case we do then we will pop the last val and add an "and" prefix before the last val instead of a comma if (count($timeAgoStrings) > 1) { $lastString = array_pop($timeAgoStrings); $timeAgoStr = implode(", ",$timeAgoStrings)." and $lastString ago"; } else { $timeAgoStr = $timeAgoStrings[0]." ago"; }
答案 6 :(得分:-1)
做数学。
else if($seconds < 120)
$interval = "1 minute ago";
else if($seconds < 172800)
$interval = "1 day ago"
等