php - 客户端和服务器的时间/日期差异?

标签 php mysql

如果之前有人问过这个问题或者我无法很好地解释,我很抱歉。我花了几个小时试图解决这个问题,但似乎无法解决这个问题。该代码在本地运行良好,但是当我将其上传到我的服务器时,服务器如何处理/检查服务器和客户端之间的时间差异似乎存在某种问题。不知道到底该怎么想或怎么想。我还知道我错误地将数据插入到 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");
      }
    }
  }

如果我缺少任何需要分享的信息,请告诉我。

最佳答案

从服务器获取时间而不是使用 time() 函数可以解决您的问题。

要么进行单独的查询

SELECT NOW() 

或将其添加到您现有的查询

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

关于php - 客户端和服务器的时间/日期差异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38612367/

相关文章:

php - 在sql中选择查询

php - 多 PHP 如何在 Ubuntu 14.04 上安装带有 PEAR 和扩展的 php 5.3

php - Illuminate\Container\Container::get($id) 的声明必须与 Psr\Container\ContainerInterface::get(string $id) 兼容

mysql - 有 INNER JOIN 限制 - 第 2 部分吗?

php - 来自 Php 和数据库的嵌套 JSON 文件

php - Laravel - 检查帐户何时创建

mysql - 优化缓慢的 MySQL 查询以删除 Using where;使用临时的;使用文件排序

MySQL时间戳选择日期范围

php - MySql UNION 具有应用默认值的不同列?

mysql - 考虑在 Rails 上进行扩展,您会编写手写 SQL 吗?使用续集 gem ?