Mysql / Php - 当前日期和时间

时间:2022-10-28 22:46:36

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.

我有一个查询,我想用当前日期时间更新列。我想要更新的列是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);
}

3 个解决方案

#1


20  

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,这将在记录更改时自动更新到当前时间。

#2


8  

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”)

#3


2  

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缓存。

#1


20  

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,这将在记录更改时自动更新到当前时间。

#2


8  

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”)

#3


2  

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缓存。