[英]Storing time in 24 Hr time-format into database with column type time
I am trying to save time into MySQL database. 我试图节省时间到MySQL数据库。 I have kept the column type as
time
in database. 我将列类型保留为数据库中的
time
。
From the form I am getting the $_POST
for time in 24-Hr format. 从表格中,我获得了24小时格式的
$_POST
时间。
The value from the form for time is as follows: 时间表中的值如下:
$_POST[openTime] = 20:00
When I convert the time by using strtotime()
and date()
I get the output as: 当我使用
strtotime()
和date()
转换时间时,输出为:
$_POST['openTime'] = date( "h:i:s A", strtotime($_POST['openTime']));
Output: 输出:
$_POST[openTime] => 08:00:00 PM
So I have a three part question: 所以我有一个三部分的问题:
Am I doing it in the correct way? 我是否以正确的方式进行操作?
Can I store the values of $_POST
directly into database after the steps I have performed above? 完成上述步骤后,是否可以将
$_POST
的值直接存储到数据库中? If no what processing is needed before I store into the database? 如果在存储到数据库之前不需要什么处理?
so here after the above changes to $_POST['openTime']
the value is: $_POST[openTime] => 08:00:00 PM
因此,在上述更改为
$_POST['openTime']
之后,此处的值为: $_POST[openTime] => 08:00:00 PM
Please note that there will database operations based on time as well. 请注意,数据库操作也会基于时间进行。 For example: "which all places are open after 8PM?"
例如:“哪些地方晚上8点以后开放?”
MySQL TIME
field type has format like HH:MM:SS
, range from -838:59:59
to 838:59:59
. MySQL
TIME
字段类型的格式类似HH:MM:SS
,范围从-838:59:59
到838:59:59
。 Read more here . 在这里阅读更多 。 Based on that, you cannot store formated time like
08:00:00 PM
. 基于此,您无法存储格式化时间,例如
08:00:00 PM
。
If you have posted open time as 20:00
, then you need to use H:i:s
format: 如果您将开放时间发布为
20:00
,则需要使用H:i:s
格式:
$_POST['openTime'] = date("H:i:s", strtotime($_POST['openTime']));
But IMHO this is unnecessary. 但是恕我直言,这是不必要的。 What you need is a validation, smth like this:
您需要的是验证,如下所示:
if (preg_match('~^\d{2}:\d{2}$~', $_POST['openTime'])) {
echo "posted time is valid!";
$saveTime = $_POST['openTime'] . ':00';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.