views:

75

answers:

4

I have a query that I want to update a collum with the current date time. The collum 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);
}
+4  A: 

Using NOW() in the query would provide this.

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.

Brad F Jacobs
woooooo. It works, You get best answer when I can.
Just keep in mind that mySql has issues with time units smaller than seconds. There are a few unsupported patches if you need this feature. You might want to just feed system time while building your request string and work everything around an epoch + macroseconds format (as many MySQL people with auditing needs are doing)
A: 

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.

Leon
A: 
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")

Maulik Vora
A: 

You can either use NOW() as premiso says as long as the MySQL server is running at the same time / in the same time zone, otherwise using a PHP function like gmdate() as Maulik mentions is a better idea.

It's best to be consistent if there's ever a case where the MySQL and the PHP servers may differ for some reason - that is, if you store timeouts by doing date mathematics in PHP ($dt=strtotime("+20 minutes");), then store the date from PHP, but if you store timeouts by doing MySQL date math (SET last_ap_date=NOW() + INTERVAL 20 minute) continue using MySQL functions.

Rudu