时间下拉列表:html和php分别为小时和分钟

时间:2021-09-13 02:56:40

I created two dropdown list for manage time. First dropdown list to manage hour in 24 hours format (Office Time) is 08.00-21.00. The second dropdown list is minutes 00-59. I blank.

我为管理时间创建了两个下拉列表。第一个以24小时格式(办公时间)管理小时的下拉列表是08.00-21.00。第二个下拉列表是00-59分钟。我空白

 <select id="Hour" class="input-small">
    <?php
       for ($i=8; $i<=21; $i++){
         echo "<option value='".$i."'>" . $i ."</option>";
       }
    ?>
 </select>

 :

 <select id="Minutes" class="input-small">
    <?php

        for ($i=0; $i<=59; $i++){
             echo "<option value='".$i."'>" . $i ."</option>";
      }
    ?>
 </select>

How to created this in php code ?

如何在PHP代码中创建这个?

Sorry, the problem is in hour format : let see. if 01, it printed 1. So, if user wanna to insert 01.00, it failed because the result is: 1.0 . It gives me error when I insert to database.

对不起,问题是小时格式:让我们看一下。如果是01,则打印1.因此,如果用户想插入01.00,则失败,因为结果是:1.0。当我插入数据库时​​,它给我错误。

2 个解决方案

#1


If you want to keep format : 01,02, ... you can make a function in php after submitting form to add 0 before the value under 10. Like :

如果你想保持格式:01,02,...你可以在提交表单后在php中创建一个函数,在10以下的值之前添加0。

if($_POST['Hour'] < 10 ){
    $concat = "0".$_POST['hour'];
}

But it's not very clean. You can use str_pad() : http://php.net/manual/fr/function.str-pad.php

但它不是很干净。您可以使用str_pad():http://php.net/manual/fr/function.str-pad.php

(Thanks Raptor for the comment)

(感谢Raptor的评论)

#2


Your suggested format (08.00-21.00) is incompatible with database (well, I assume you're using MySQL). You didn't mention what is the data type of the column, but as in your case, you should be storing the information as VARCHAR.

您建议的格式(08.00-21.00)与数据库不兼容(好吧,我假设您使用的是MySQL)。您没有提到列的数据类型是什么,但在您的情况下,您应该将信息存储为VARCHAR。

To format the post values to your suggested format, you can use the following:

要将帖子值格式化为建议的格式,您可以使用以下内容:

$open_time = str_pad($_POST['hour'], 2, '0', STR_PAD_LEFT) . '.' . str_pad($_POST['minute'], 2, '0', STR_PAD_LEFT);

p.s. you should give a name to your input <select>

附:你应该为输入

#1


If you want to keep format : 01,02, ... you can make a function in php after submitting form to add 0 before the value under 10. Like :

如果你想保持格式:01,02,...你可以在提交表单后在php中创建一个函数,在10以下的值之前添加0。

if($_POST['Hour'] < 10 ){
    $concat = "0".$_POST['hour'];
}

But it's not very clean. You can use str_pad() : http://php.net/manual/fr/function.str-pad.php

但它不是很干净。您可以使用str_pad():http://php.net/manual/fr/function.str-pad.php

(Thanks Raptor for the comment)

(感谢Raptor的评论)

#2


Your suggested format (08.00-21.00) is incompatible with database (well, I assume you're using MySQL). You didn't mention what is the data type of the column, but as in your case, you should be storing the information as VARCHAR.

您建议的格式(08.00-21.00)与数据库不兼容(好吧,我假设您使用的是MySQL)。您没有提到列的数据类型是什么,但在您的情况下,您应该将信息存储为VARCHAR。

To format the post values to your suggested format, you can use the following:

要将帖子值格式化为建议的格式,您可以使用以下内容:

$open_time = str_pad($_POST['hour'], 2, '0', STR_PAD_LEFT) . '.' . str_pad($_POST['minute'], 2, '0', STR_PAD_LEFT);

p.s. you should give a name to your input <select>

附:你应该为输入