简体   繁体   English

在MySQL日期时间表示中添加N小时

[英]Add N hours to MySQL datetime representation

How can I add N more hours to MySQL datetime representation? 如何在MySQL日期时间表示中再添加N个小时? For example Current: 2013-12-01 19:30:13 (or I can get it with date("Ymd H:i:s") in PHP) 例如Current:2013-12-01 19:30:13(或者我可以用PHP获得日期(“Ymd H:i:s”))

How can I get this: 2013-12-01 22:30:13 (adding 3 more hours)? 我怎么能得到这个:2013-12-01 22:30:13(又增加3个小时)?

date("Ymd H:i:s") + 3 isn't working in PHP date(“Ymd H:i:s”)+ 3在PHP中不起作用

在PHP中:

$new_time = date("Y-m-d H:i:s", strtotime('+3 hours');

In MySQL you can use date_add 在MySQL中,您可以使用date_add

From the docs: 来自文档:

DATE_ADD(date,INTERVAL expr unit) DATE_ADD(日期,INTERVAL expr单位)

These functions perform date arithmetic. 这些函数执行日期算术。 The date argument specifies the starting date or datetime value. date参数指定开始日期或日期时间值。 expr is an expression specifying the interval value to be added or subtracted from the starting date. expr是一个表达式,用于指定要从开始日期添加或减去的区间值。 expr is a string; expr是一个字符串; it may start with a “-” for negative intervals. 对于负间隔,它可能以“ - ”开头。 unit is a keyword indicating the units in which the expression should be interpreted. unit是一个关键字,表示应该解释表达式的单位。

The INTERVAL keyword and the unit specifier are not case sensitive. INTERVAL关键字和单位说明符不区分大小写。

For your case you can do: 对于您的情况,您可以这样做:

date_add(now(), interval 3 HOUR)

sqlfiddle demo sqlfiddle演示

It depends on where you want to do it. 这取决于你想要去哪里。

in mysql 在mysql中

SELECT NOW() + INTERVAL 3 HOUR

SELECT CAST('2013-12-01 23:49:09' AS DATETIME) + INTERVAL 3 HOUR

in php 在PHP中

Date("Y-m-d h:i:s", time() + 3600 * 3)

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

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