以人类可读的格式表达时差
在下面的函数中,我使用了 2 个相同的变量,因为里面是不同的语言,我需要帮助来替换这个变量 $periods[$j] .= "";
例子:
function showdate($time)
{
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$periods2 = array("seconds", "minutes", "hours", "days", "weeks", "months", "years", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
$now = time();
$difference = $now - $time;
$tense = "ago";
for ($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++)
{
$difference /= $lengths[$j];
}
$difference = round($difference);
if ($difference != 1)
{
**/* In this case, I need to show this variable: $periods2 */**
$periods[$j] .= "";
}
return "$difference $periods[$j] $tense";
}
On following function, I used 2 identical variables because inside is different language, I need help to replace this variable $periods[$j] .= "";
example:
function showdate($time)
{
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$periods2 = array("seconds", "minutes", "hours", "days", "weeks", "months", "years", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
$now = time();
$difference = $now - $time;
$tense = "ago";
for ($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++)
{
$difference /= $lengths[$j];
}
$difference = round($difference);
if ($difference != 1)
{
**/* In this case, I need to show this variable: $periods2 */**
$periods[$j] .= "";
}
return "$difference $periods[$j] $tense";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以只写
$periods[$j] = $periods2[$j]
,但我认为创建另一个变量更好。You can just write
$periods[$j] = $periods2[$j]
, but I think making another variable is better.另外,periods2 数组中的十年缺少 s。
Also, decade in your periods2 array is missing an s.