MySQL / PHP - 时间过去了

时间:2010-09-01 23:00:08

标签: php mysql datetime

如何计算日期时间类型传递的5分钟时段?

有可能吗?

//Update AP (+1 points every 5 mins)

//          PLAN OF ACTION (for ap update)!
// 1. check how long it has been since last update 
// 2. calculate how many 5 min periods has passed 
// 3. update $ap accordinly 
if ($last_ap_update != "0000-00-00 00:00:00"){ //Make sure last_ap_update has a value


}
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($query);
}

1 个答案:

答案 0 :(得分:1)

从当前的unix时间戳减去过去的unix时间戳,然后将结果除以300(60 * 5)。

您可以通过以下任一方式将MySQL日期时间转换为unix时间戳:

  • 使用SQL UNIX_TIMESTAMP 功能,即SELECT UNIX_TIMESTAMP(last_ap_update)

  • 将日期时间字符串传递给 PHP的strtotime,即strtotime('2009-10-31 12:59:59');