php 在Mysql中插入日期和时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/2930059/
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
Insert date and time into Mysql
提问by FlyingCat
I am trying to insert date and time into mysql datetime field. When a user select a date and time, it will generate two POST variables. I have searched internet but still not sure how to do it.
我正在尝试将日期和时间插入到 mysql 日期时间字段中。当用户选择日期和时间时,它会生成两个 POST 变量。我已经在互联网上搜索过,但仍然不知道该怎么做。
My code.
我的代码。
//date value is 05/25/2010
//time value is 10:00
$date=$_POST['date'];
$time=$_POST['time'];
$datetime=$date.$time
If I insert $datetime into mysql, the date appears to be 0000-00-00:00:00:00
如果我将 $datetime 插入 mysql,日期似乎是 0000-00-00:00:00:00
I appreciate it if anyone could help me about this. Thanks.
如果有人可以帮助我,我将不胜感激。谢谢。
采纳答案by GSto
$datetime = $_POST['date'] . ' ' . $_POST['time'] . ':00';
$datetime = mysql_real_escape_string($datetime);
$query = "INSERT INTO table(timestamp) VALUES ('$datetime')";
alternative solution that can handle more formats:
可以处理更多格式的替代解决方案:
$datetime = $_POST['date'] . ' ' . $_POST['time'];
$datetime = mysql_real_escape_string($datetime);
$datetime = strtotime($datetime);
$datetime = date('Y-m-d H:i:s',$datetime);
$query = "INSERT INTO table(timestamp) VALUES ('$datetime')";
回答by Your Common Sense
Date must have format shown to you: 0000-00-00 00:00:00
日期必须具有向您显示的格式: 0000-00-00 00:00:00
So, you have to convert it. There are thousand questions about this conversion here on SO.
The shortest way is like
所以,你必须转换它。关于此转换,SO 上有数千个问题。
最短的方法就像
list($m,$d,$y) = explode("/",$_POST['date']);
$date = mysql_real_escape_string("$y-$m-$d ".$_POST['time']);
回答by vicatcu
I think the datetime format looks like this:
我认为日期时间格式如下所示:
YYYY-MM-DD HH:MM:SS
so you've got to format your $datetime to look like that. And in your query that needs to be encapsulated in quoation marks.
所以你必须格式化你的 $datetime 看起来像那样。并且在您的查询中需要封装在引号中。
回答by Konerak
- Either you transform the string to be in the YYYY-MM-DD HH:MM:SSformat yourself,
- or you use the - str_to_date()function from MySQL.- INSERT INTO table DATETIME values (str_to_date($date,"%m/%d/%Y %h:%i")) 
- 要么您自己将字符串转换为YYYY-MM-DD HH:MM:SS格式,
- 或者您使用 - str_to_date()MySQL 中的函数。- 插入表 DATETIME 值 (str_to_date($date,"%m/%d/%Y %h:%i")) 
回答by a1ex07
As far as I remember, by default, Mysql datetime should be in "yyyy-mm-dd hh:mm:ss" format.
据我所知,默认情况下,Mysql 日期时间应采用“yyyy-mm-dd hh:mm:ss”格式。
回答by Sruthi.user3337197
I have added date and time in mysql db via API like this:
我通过这样的 API 在 mysql db 中添加了日期和时间:
Call API
调用接口
http://localhost/SendOrder.php?odate=20120323&otime=164545
http://localhost/SendOrder.php?odate=20120323&otime=164545
- odatetype is DATE
- otimetype is TIME in DB.
- odate类型是日期
- otime类型是 DB 中的 TIME。
It will store odate as 2012-03-23 and otime as 16:45:45.
它会将 odate 存储为 2012-03-23,将 otime 存储为 16:45:45。

