[英]How to get mysql data from table into php date()?
標題很奇怪,是的。 (我是挪威文)
好的,我正在為新項目創建一個計數器/計時器,它的作用是當用戶執行操作時,他/她必須等待100秒才能再次執行操作。 但是,如何再次將時間戳記(保存在mysql db中)保存到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);
$time = date("h:i:s", time() + 100);
$time_now = strtotime("now");
$time_to_check = strtotime($row["time"]) + 100;
您可以在MySQL中將日期轉換為UNIX時間戳:
SELECT UNIX_TIMESTAMP(time) AS time FROM `crimes` ...
然后在PHP中:
$seconds_ago = time() - $row['time'];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.