Mysql/Php - 当前日期和时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3618401/
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
Mysql/Php - Current date and time
提问by user377419
I have a query that I want to update a column with the current date time. The column I want to update is of the type datetime.
我有一个查询,我想用当前日期时间更新一列。我要更新的列是日期时间类型。
How can I get it and set it?
我怎样才能得到它并设置它?
else{ //If the last update is NULL, It must be players first page load. So set datetime equal to NOW
$query = "UPDATE `stats` SET `last_ap_update` = WHERE `member_id` = {$_SESSION['SESS_MEMBER_ID']}";
$queryresult = mysql_query($insertqry);
}
回答by Jim W.
Using NOW()
in the query would provide this.
NOW()
在查询中使用将提供这一点。
else{ //If the last update is NULL, It must be players first page load. So set datetime equal to NOW
$query = "UPDATE `stats` SET `last_ap_update` = NOW() WHERE `member_id` = {$_SESSION['SESS_MEMBER_ID']}";
$queryresult = mysql_query($insertqry);
}
But if this gets updated anytime the table updates, I would suggest changing the column to TIMESTAMP
and this will automatically update to the current time for you anytime that record changes.
但是,如果在表更新时随时更新,我建议将列更改为TIMESTAMP
,这将在记录更改时自动更新为当前时间。
回答by Maulik Vora
else{ //If the last update is NULL, It must be players first page load. So set datetime equal to NOW
$query = "UPDATE `stats` SET `last_ap_update` = '".gmdate("Y-m-d H:i:s")."' WHERE `member_id` = {$_SESSION['SESS_MEMBER_ID']}";
$queryresult = mysql_query($insertqry);
}
You can use any format you want instead of gmdate("Y-m-d H:i:s")
您可以使用任何您想要的格式,而不是 gmdate("Y-m-d H:i:s")
回答by Leon
You can use the mysql function NOW() for this, or you can pase the $_SERVER['REQUEST_TIME'] in there so the query gets cached by mysql.
您可以为此使用 mysql 函数 NOW(),或者您可以在其中传递 $_SERVER['REQUEST_TIME'] 以便查询被 mysql 缓存。