2016-07-27 115 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尝试它。不幸的是,在尝试之后,我得到了相同的结果。它似乎仍然在某种程度上有所不同。尽管谢谢你的回复。 –

相关问题