2012-05-01 35 views
0

標題很奇怪,是的。 (Im norwegian)如何從表中獲取mysql數據到php date()?

好的,我正在爲我的新項目製作一個計數器/計時器,它做了什麼,當用戶做出動作時,他/她必須等待100秒才能再次執行。但是,如何將時間戳記(保存在MySQL數據庫中)再次輸入到PHP代碼中,並檢查用戶上次檢查再次嘗試的時間。

我的一些代碼(從腳本):

// Waiting for time to go? 
$sql = mysql_query("SELECT * FROM `crimes` WHERE user_id = '".$_SESSION["id"]."'"); 
$num = mysql_num_rows($sql); 
$row = mysql_fetch_array($sql); 

if($num == 1){ //Waiting 
    $time_now = date("H:i:s", strtotime("+100 seconds")); // This is where Im stuck, how to get that timestamp from mysql with 100 secs added? 
    $time_to_check = date("H:i:s", $row["time"]); // The time the action was done. 

    // This is where the code is going to check how long time since user visited. 



} else {   

} 

那麼,什麼林要求是,我需要的是從MySQL中包含的數據與100秒增加一個變種,怎麼樣? :)

這工作:

$sql2 = mysql_query("SELECT UNIX_TIMESTAMP(time) AS time FROM `crimes` WHERE user_id = '".$_SESSION["id"]."'"); 
$row2 = mysql_fetch_array($sql2); 
$seconds_ago = time() - $row2['time']; 
$time_now = date("H:i:s", $seconds_ago + 100); 

回答

0

您可以轉換日期以UNIX時間戳在MySQL:

SELECT UNIX_TIMESTAMP(time) AS time FROM `crimes` ... 

內。然後PHP:

$seconds_ago = time() - $row['time']; 
+0

Works,with $ seconds_ago = time() - $ row2 ['time']; $ time_now = date(「H:i:s」,$ seconds_ago + 100);. 謝謝。 – hultberg

0
$time = date("h:i:s", time() + 100); 
+0

好的,但是在哪裏插入MySQL的$ row [「time」]? – hultberg

0
$time_now = strtotime("now"); 
$time_to_check = strtotime($row["time"]) + 100;