以人类可读的格式表达时差

发布于 2024-12-02 15:28:41 字数 890 浏览 1 评论 0原文

在下面的函数中,我使用了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼泪淡了忧伤 2024-12-09 15:28:41

您可以只写 $periods[$j] = $periods2[$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);  
    $pText = $periods[$j];
    if($difference>1) $pText = $periods2[$j];
    return "$difference $pText $tense";  
}

You can just write $periods[$j] = $periods2[$j], but I think making another variable is better.

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);  
    $pText = $periods[$j];
    if($difference>1) $pText = $periods2[$j];
    return "$difference $pText $tense";  
}
安人多梦 2024-12-09 15:28:41
if($difference != 1)   
{
     $periods[$j] = $periods2[$j];
}  

另外,periods2 数组中的十年缺少 s。

if($difference != 1)   
{
     $periods[$j] = $periods2[$j];
}  

Also, decade in your periods2 array is missing an s.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文