简体   繁体   English

如何在 12 小时内更改此 24 小时时间格式

[英]How to change this 24 hours time format in 12 Hours

Hi i am facing a problem while editing the plugin.嗨,我在编辑插件时遇到问题。 the default time format of this plugin is 24 hours but i want to convert it into 12 hours with AM/PM as well.此插件的默认时间格式是 24 小时,但我也想将其转换为上午/下午的 12 小时。 Here is the code that i have这是我拥有的代码

<div class="col-xs-4 col-sm-4 col-md-4">
    <div class="form-group qc-input-container">
        <select name="quickcab_form_departure_time_hour" id="quickcab_form_departure_time_hour" class="booking-input quickcab-select-input form-control" required>
            <option disabled selected><?php echo esc_html__('Hour', 'quickcab'); ?></option>
<?php
for ( $i = 1; $i <= 12; $i++ ) {
?>
            <option value="<?php echo sprintf('%02d', $i); ?>"><?php
                      echo sprintf('%02d', $i);
                    ?></option>
<?php
}
?>
        </select>
    </div>

You can easily get the 12 hours time by using date() function.您可以使用date()函数轻松获取 12 小时时间。 I'm giving an example for you.我给你举个例子。

// suppose your time is 19:24:15
$date = '19:24:15'; 
echo date('h:i:s a', strtotime($date));

The output will be输出将是

07:24:15 pm

You can get the output also by using DateTime您也可以使用DateTime获取输出

$date = new DateTime('19:24:15');
echo $date->format('h:i:s a') ;

h is used for 12 digit time h用于 12 位时间
i stands for minutes代表分钟
s seconds s
a will return am or pm (use in uppercase for AM PM) a将返回 am 或 pm(在 AM PM 中使用大写)

To keep it really simple you could just add another loop to 12 to get a full 24 hours, then a simple change to the sprinf() will get you the AM and PM, like this为了保持简单,您可以在 12 中添加另一个循环以获得完整的 24 小时,然后对 sprinf() 进行简单更改即可获得 AM 和 PM,如下所示

sprintf('%02dAM', $i);

and in the second loop在第二个循环中

sprintf('%02dPM', $i)

So your code所以你的代码

<div class="col-xs-4 col-sm-4 col-md-4">
<div class="form-group qc-input-container">
    <select name="quickcab_form_departure_time_hour" id="quickcab_form_departure_time_hour" class="booking-input quickcab-select-input form-control" required>
        <option disabled selected><?php echo esc_html__('Hour', 'quickcab'); ?></option>
<?php
// first 12 hours
for ( $i = 1; $i <= 12; $i++ ) {
?>
        <option value="<?php echo sprintf('%02dAM', $i); ?>"><?php echo sprintf('%02dAM', $i);?></option>
<?php
}
// second 12 hours
for ( $i = 1; $i <= 12; $i++ ) {
?>
        <option value="<?php echo sprintf('%02dPM', $i); ?>"><?php echo sprintf('%02dPM', $i);?></option>
<?php
}
?>

    </select>
</div>

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

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