PHP MYSQL 手动更新数据库中的日期和时间字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6617023/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:51:41  来源:igfitidea点击:

PHP MYSQL update a DATE and TIME field in db manually

phpmysqldatabase

提问by daza166

I am trying to figure out how to update a MYSQL DATE and TIME field manually (NOT to todays date!)ie set it the date field to a certain date value and time field to a certain time field correctly in the correct SQL required field format.

我想弄清楚如何手动更新 MYSQL DATE 和 TIME 字段(不是今天的日期!)即将日期字段设置为某个日期值,并将时间字段正确设置为某个时间字段,以正确的 SQL 必填字段格式.

I am using an UPDATE query not INSERT as my action is to update a users field

我使用的是 UPDATE 查询而不是 INSERT 因为我的操作是更新用户字段

Done some research and came up with something along the lines of (obviously this example wont work but does anyone know how to format this query?

做了一些研究并想出了一些类似的东西(显然这个例子不起作用,但有谁知道如何格式化这个查询?

UPDATE mytblname SET date='DATE: Manual Date', '2011-06-14'', time='TIME: Manual Time', '12:10:00' WHERE email='somevalue'

If I just enter the value as normal SQL way it gives 0000-00-00 for date and 00:00:00 for time - ie

如果我只是以普通 SQL 方式输入值,它会为日期提供 0000-00-00,为时间提供 00:00:00 - 即

SET date='2011-06-14',time='12:33:35'

Thanks for any suggestions, really appreciate it!!!

感谢您的任何建议,非常感谢!!!

回答by Brad

UPDATE mytblname SET `date`="2011-06-14", `time`="12:10:00" WHERE email="somevalue";

This should work fine. Make sure you have the appropriate backticks around dateand time.

这应该可以正常工作。确保date和周围有适当的反引号time

回答by Tadeck

Please refer to the MySQL Documentation on DATE, TIME and DATETIME formats. You can see there, that there are multiple possibilities of the values that can be assigned to fields of these types.

请参阅有关 DATE、TIME 和 DATETIME 格式MySQL 文档。您可以在那里看到,可以将值分配给这些类型的字段有多种可能性。

So this should work:

所以这应该有效:

UPDATE `mytblname` SET `date`=NOW(), `time`=NOW() WHERE `email`='somevalue';

or to any specific date like that (the string will be automatically converted to DATE, TIME or DATETIME format):

或任何类似的特定日期(字符串将自动转换为 DATE、TIME 或 DATETIME 格式):

UPDATE `mytblname`
SET
`date`='1987-01-02 11:22:33',
`time`='1987-01-02 11:22:33'
WHERE `email`='somevalue';

You can also assign it like that, which is more clear:

你也可以这样赋值,这样更清楚:

UPDATE `mytblname`
SET
`date`='1987-01-02',
`time`='11:22:33'
WHERE `email`='somevalue';

The only question is, which path will you choose :)

唯一的问题是,你会选择哪条路:)