从 PHP 在 MySql 中写入当前时间加 5 分钟

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

Write current time plus 5 minutes in MySql from PHP

phpmysql

提问by user2840278

Helllo, I have a query that writes some data to the database

你好,我有一个查询将一些数据写入数据库

mysql_query ("INSERT INTO PVS (time_ended) 
VALUES (now())", $db_conx) 
or die (mysql_error());

mysql_query ("UPDATE PVS SET break = now() WHERE id = '$id'", $db_conx)
or die (mysql_error());

What I want is to store current time + 5 minutes instead of now() for time_ended, and current time + 15 seconds instead of now() for break

我想要的是为 time_ended 存储当前时间 + 5 分钟而不是 now(),为中断存储当前时间 + 15 秒而不是 now()

Can someone show me the trick? Thank you!

有人可以告诉我诀窍吗?谢谢!

回答by Shankar Damodaran

Try like this

像这样尝试

mysql_query ("INSERT INTO PVS (time_ended) 
VALUES ((now() + INTERVAL 5 MINUTE))", $db_conx) 
or die (mysql_error());

and

mysql_query ("UPDATE PVS SET break = (now() + INTERVAL 5 SECOND) WHERE id = '$id'", $db_conx)
or die (mysql_error());

回答by Jason Heo

this DATE_ADD()help:

这个DATE_ADD()帮助:

SELECT DATE_ADD(NOW(), INTERVAL 5 SECOND);

store current time + 5 minutes instead of now() for time_ended

为 time_ended 存储当前时间 + 5 分钟而不是 now()

INSERT INTO PVS (time_ended) VALUES (DATE_ADD(NOW(), INTERVAL 5 MINUTE)

current time + 15 sec

当前时间 + 15 秒

UPDATE PVS SET break = DATE_ADD(now(), INTERVAL 15 SECOND) WHERE id = '$id'

and all possible INTERVAL

以及所有可能的 INTERVAL

you can find at http://www.w3schools.com/sql/func_date_add.asp

你可以在http://www.w3schools.com/sql/func_date_add.asp找到