簡體   English   中英

在 WordPress 簡碼中將 24 小時數字轉換為時間

[英]Convert a 24hr number to time in a WordPress shortcode

我正在嘗試獲取一個 4 位數字(例如 0900)並將其轉換為時間。 我采取了只是格式化數字並添加冒號的路線,這很好,直到我意識到需要在上午/下午的 12 小時內成為 output。 我將 function 包裝在 WordPress 短代碼中,以將其用於自定義字段 output。

function hel_format_time($atts, $content = null) {
    $content = do_shortcode($content);
    $time = substr_replace($content, ':', 2, 0);
    return $time;
}
add_shortcode('format-time', 'hel_format_time');

這會將 0900 的內容翻譯成 09:00,但我需要的是 0900 到上午 9:00。

看起來您正在嘗試將軍隊時間轉換為上午/下午格式。 如果是這樣,請。 嘗試這個。

function hel_format_time($atts, $content = null) {
    $content = do_shortcode($content);
    $time = date("h:i a", strtotime($content));
    return $time;
}
add_shortcode('format-time', 'hel_format_time');

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM