简体   繁体   English

如何在MySQL时间戳中设置时间?

[英]How do I set the time in a MySQL timestamp?

How do I change just the time in a timestamp (say, from 2014-08-08 14:26:15 to 2014-08-08 08:00:00)? 如何更改时间戳中的时间(例如,从2014-08-08 14:26:15到2014-08-08 08:00:00)? In PHP I would probably get the timestamp from MySQL, modify the time, then update the table, but I figured there might be a function for doing so in MySQL alone. 在PHP中,我可能会从MySQL获取时间戳,修改时间,然后更新表,但我认为仅在MySQL中可能就有这样做的功能。

(I know timestamps are not used for this purpose, and that a datetime would be better, but I don't have control over the DB schema.) (我知道不将时间戳用于此目的,并且日期时间会更好,但我无法控制数据库模式。)

You can format a date (or datetime) into a datetime string, and specify a hard-coded time, ignoring any time component that may be in the original value. 您可以将日期(或日期时间)格式化为日期时间字符串,并指定硬编码时间,而忽略原始值中可能存在的任何时间分量。

DATE_FORMAT(datecolumn, '%Y-%m-%d 08:00:00')

See http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_date-format for reference doc on that DATE_FORMAT() function. 有关该DATE_FORMAT()函数的参考文档,请参见http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_date-format

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM