簡體   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