2016-07-27 118 views
0

對不起,如果之前已經詢問過,或者我無法以很好的方式解釋。我花了好幾個小時試圖繞過這個問題,而我似乎無法解決這個問題。代碼在本地工作正常,但是當我將它上傳到我的服務器時,服務器如何處理/檢查服務器和客戶端之間的時間差異似乎存在某種問題。不確定如何或想什麼。我也意識到我不正確地將數據插入到pdo語句中,目前我不在意這一點。稍後我會整理所有這些。與客戶端和服務器的時間/日期差異?

我遇到問題的應用程序的部分是檢查活動的貨件(這是某種遊戲)。我想比較數據庫中的時間戳和當前時間,看看時間是否已經過去。在到貨時間過去的情況下,我希望將貨件的狀態設置爲「交付」,並將數據添加到另一個名爲「存儲」的表中。

function check_active_shipment(){ 
    $pdo = pdo(); 
    $username = $_SESSION['username']; 
    $statement = $pdo->prepare("SELECT * FROM shipments WHERE username LIKE '$username' AND status LIKE 'active' LIMIT 1"); 
    $statement->execute(); 
    $rows = $statement->fetch(); 
    $id = $rows['id']; 

    if($rows['type'] == "purchase"){ 
     if(time() >= strtotime($rows['arrival'])){ 
     $statement = $pdo->prepare("UPDATE shipments SET status='delivered' WHERE id LIKE '$id'"); 
     $statement->execute(); 

     $statement = $pdo->prepare("INSERT INTO storage (username, crate_type_id, quantity) VALUES (?, ?, ?)"); 
     $statement->bindParam(1, $username); 
     $statement->bindParam(2, $rows['crate_type_id']); 
     $statement->bindParam(3, $rows['quantity']); 
     $statement->execute(); 
     //header("location:index.php"); 
     } 
    } 
    else if($rows['type'] == "sale"){ 
     if(time() >= strtotime($rows['arrival'])){ 
     $statement = $pdo->prepare("UPDATE shipments SET status='delivered' WHERE id LIKE ?"); 
     $statement->bindParam(1, $id); 
     $statement->execute(); 

     $statement = $pdo->prepare("SELECT cash FROM users WHERE username LIKE ?"); 
     $statement->bindParam(1, $username); 
     $statement->execute(); 
     $user = $statement->fetch(); 
     $cash = $user['cash'] + $rows['value']; 

     $statement = $pdo->prepare("UPDATE users SET cash=?"); 
     $statement->bindParam(1, $cash); 
     $statement->execute(); 
     //header("location:index.php"); 
     } 
    } 
    } 

讓我知道是否有任何信息我想失去分享。

+0

是您的數據庫託管在其他服務器上?如果是這樣,它是否託管在具有不同時間/時區的服務器上? –

+0

查詢時區。 – eronax59

+0

一切都在同一臺服務器,文件和數據庫上。我不知道它是什麼時區,但考慮到服務器在斯德哥爾摩我假設歐洲/斯德哥爾摩。關於如何找出服務器/數據庫時區的任何想法? –

回答

0

將獲取時間從服務器,而不是使用時間()功能,可以解決你的問題。

要麼是一個單獨的查詢

SELECT NOW() 

或將其添加到現有的查詢

SELECT *, NOW() as curtime FROM shipments WHERE ..... 
+0

我不知道爲什麼我沒有想到這一點,但這有效奇蹟!謝謝PaulF! –

0
您是否嘗試過使用 DateTime類?也許它的表現很像你曾預計...以下是如何在你的情況下,使用它的代碼段:
<?php 
     // ...SOME CODE... 
     if($rows['type'] == "purchase"){ 
      // TRY USING DateTime CLASS 
      $arrival = new DateTime($rows['arrival']); 
      $arrivalTS = $arrival->getTimestamp(); 

      if(time() >= $arrivalTS){ 
       // REST OF THE CODE... 
      } 

     }else if($rows['type'] == "sale") { 
      // TRY USING DateTime CLASS AGAIN 
      $arrival = new DateTime($rows['arrival']); 
      $arrivalTS = $arrival->getTimestamp(); 

      if (time() >= $arrivalTS) { 
       // REST OF THE CODE... 
      } 
     } 
+0

我嘗試過使用DateTime類,儘管我沒有用getTimestamp嘗試它。不幸的是,在嘗試之後,我得到了相同的結果。它似乎仍然在某種程度上有所不同。儘管謝謝你的回覆。 –

相關問題