php 如何将 am/pm 格式的时间插入到数据库中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13707206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert time with am/pm format into the database
提问by Towki
So im trying to insert a time using an input text field into a database table with data type TIME.
因此,我尝试使用输入文本字段将时间插入到数据类型为 TIME 的数据库表中。
The format of time that I want to insert should be like this:
我想插入的时间格式应该是这样的:
H:MM pm// example: 6:30 pm
H:MM pm// 示例:6:30 pm
The problem is the am/pm doesnt insert in my database table. Only the hour and minute.
问题是 am/pm 没有插入我的数据库表中。只有小时和分钟。
Please give me idea how to solve this. Better with sample codes. Thanks.
请告诉我如何解决这个问题。使用示例代码更好。谢谢。
回答by shadyyx
Data Type TIMEis for storing time data type - that means no AM/PM. Store the data in Your database in 24 hour format and format it to 12 hour format with am/pm in PHP or MySQL using one of these:
数据类型TIME用于存储时间数据类型 - 这意味着没有 AM/PM。将数据以 24 小时格式存储在您的数据库中,并使用以下之一在 PHP 或 MySQL 中使用 am/pm 将其格式化为 12 小时格式:
PHP:
PHP:
$date = new DateTime($mysql_column['time']);
$date->format('h:i:s a');
or:
或者:
$date = date('h:i:s a', strtotime($mysql_column['time']));
or MySQL:
或 MySQL:
SELECT DATE_FORMAT('%h:%i:%s %p', time) FROM table;
回答by Madara's Ghost
Store the TIMEas a standard format (18:30:00), and the format it however you want when you display it (Using DateTimeobjects or the date functions).
将 存储TIME为标准格式 ( 18:30:00),以及显示时所需的格式(使用DateTime对象或日期函数)。
MySQL doesn't support extra formats when storing time data.
MySQL 在存储时间数据时不支持额外的格式。
回答by Debugger
I think you want to add the jquery time picker value in your database with actual format in the database.
我认为您想在数据库中使用数据库中的实际格式在数据库中添加 jquery 时间选择器值。
Here I have written some function
这里我写了一些函数
function update_time($time){
$ap = $time[5].$time[6];
$ttt = explode(":", $time);
$th = $ttt['0'];
$tm = $ttt['1'];
if($ap=='pm' || $ap=='PM'){
$th+=12;
if($th==24){
$th = 12;
}
}
if($ap=='am' || $ap=='AM'){
if($th==12){
$th = '00';
}
}
$newtime = $th.":".$tm[0].$tm[1];
return $newtime;
}
$time = update_time($_POST['time']); //here I am calling the function now you can insert the value in db
you just have to call the function and insert the returned value in database.
And while printing that you can do something like that echo date("h:i A",strtotime($time));
您只需要调用该函数并将返回值插入数据库。在打印时你可以做类似的事情echo date("h:i A",strtotime($time));
回答by Diane
try .ToShortTimeString() after your date variable.
在日期变量之后尝试 .ToShortTimeString() 。
回答by Darius
You would be best changing the field type to 'VARCHAR (32)', and then writing the time with PHP.
您最好将字段类型更改为“VARCHAR (32)”,然后使用 PHP 写入时间。
Example: date('m/d/y g:i:sa');
Why do you want to store the am or pm anyhow? If you store the date/time as a unix epoch timestamp, you can format the date however you want in the program - not the database.
你为什么要存储 am 或 pm 呢?如果您将日期/时间存储为 unix 纪元时间戳,您可以在程序中根据需要格式化日期 - 而不是数据库。
Example: time(); - Store this in an INT(8) field.
date('m/d/y g:i:sa, $time()); - Output from DB like this.
回答by imkingdavid
Change the type of the field to a varchar. TIME cannot store it like that. However, keep in mind that storing it like you want to will make it more difficult to provide localized results if that is something you will eventually need. That is, timezone support becomes difficult if you are not storing the timestamp itself, but rather a user-friendly representation.
将字段的类型更改为 varchar。TIME 不能这样存储它。但是,请记住,如果您最终需要本地化结果,那么按照您想要的方式存储它会使提供本地化结果变得更加困难。也就是说,如果您不存储时间戳本身,而是存储用户友好的表示,则时区支持变得困难。
EDIT: Or, DATETIME works as well, as was pointed out in the comments above.
编辑:或者,DATETIME 也有效,正如上面的评论中指出的那样。
回答by Theo Kouzelis
You can use the DateTime Object in PHP which has functions to create a time object from any format and also has a function to output a time in any format like so
您可以使用 PHP 中的 DateTime 对象,它具有从任何格式创建时间对象的功能,也具有以任何格式输出时间的功能,如下所示
<?php
$date = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
echo $date->format('Y-m-d');
?>

