在php日期计算中更改时间输出格式

时间:2016-10-20 15:05:31

标签: php unix timestamp

好吧,我不是一个程序员,我需要对一行代码进行一些PHP调整,并花了很多时间试图找出如何做到这一点。有很多关于时间格式的教程,但我无法找到答案。

我在应用程序中有这行代码:

<span class="muted">Expires in <?=(now() > $l->list_expires) ? 'Closed' : timespan(now(),$l->list_expires)?></span>

我发现&#39; list_expires&#39;是一个mysql列,将来有一个unix时间日期,即1479350850。 代码计算从现在到未来日期的时间并输出结果如下: Coija.com将在4周,1天,21小时,30分钟到期

我想要的是以更短的方式显示结果,比如说,&#39; 29天到期&#39;如果它少于一天,&#39;在13个小时后到期&#39;或者&#39;在10分钟后到期&#39;另一种选择是“剩下29天”。

我知道第一部分检查时间是否过期并输出“关闭”,但是现在,如果关闭,则输出为:&#39;已过期关闭&#39;。 我怎么能不显示&#39;过期&#39;如果&#39;关闭&#39;必须出示?

非常感谢任何帮助。 谢谢

2 个答案:

答案 0 :(得分:0)

对不起,我发现'timespan'不是php命令,而是脚本中的函数。现在,我开始玩那个以查看结果。

感谢您的耐心等待。

答案 1 :(得分:0)

以下是一个时间跨度函数示例:

function timespan ($current_time, $list_expires) {
    /** The formatted time span */
    $formatted_timespan = "";
    /** The difference between the current time and list expires */
    $time_difference = ($current_time - $list_expires);
    /** If the time difference is greater than 1 day */
    if ($time_difference > (3600*24)) {
        /** The number of days */
        $day_count          = floor($time_difference / (3600*24));
        $formatted_timespan = "Expires in " . $day_count . " days";
    }
    /** If the time difference is less than 1 day but larger than 1 hour */
    else ($time_difference < (3600*24) && $time_difference > (3600)) {
        /** The number of hours */
        $hour_count         = floor($time_difference / (3600));
        $formatted_timespan = "Expires in " . $hour_count . " hours";
    }
    /** If the time difference is less than 1 hour */
    else ($time_difference < (3600)) {
        /** The number of minutes */
        $minute_count       = floor($time_difference / (60));
        $formatted_timespan = "Expires in " . $minute_count . " minutes";
    }
    return $formatted_timespan;
}
相关问题