繁体   English   中英

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

[英]Change time output format in php date calculation

好的,我不是编码人员,我需要对一行代码进行一些php调整,并花了很多时间试图弄清楚该怎么做。 有很多关于时间格式的教程,但我找不到我需要的答案。

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

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

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

我想要以较短的方式显示结果,例如“ 29天后过期”,如果少于一天,则显示“ 13小时后过期”或“ 10分钟后过期”。 另一个选择是“还剩29天”。

我知道第一部分会检查时间是否过期并输出“关闭”,但是现在,如果关闭了,输出是:“关闭时过期”。 如果必须显示“已关闭”,我如何不显示“已过期”?

任何帮助将不胜感激。 谢谢

抱歉,我发现这里的“时间跨度”不是php命令,而是脚本中的函数。 现在,我开始发挥作用以查看结果。

感谢您的耐心等待。

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

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM