PHP检查用户订阅

时间:2018-10-23 06:16:28

标签: php

php下面,我想检查用户订阅日期是否已经过去。下面的代码不起作用。我的用户表看起来像(id,unique_id,名称,电子邮件,encrypted_pa​​ssword,is_active,created_at,exp_date)

<?php

require("config.php");

if (!empty($_POST)) {

  $response = array("error" => FALSE);

  $query = "SELECT * FROM users WHERE email = :email";

  $query_params = array(
    ':email' => $_POST['email']
  );

  try {
    $stmt = $db->prepare($query);
    $result = $stmt->execute($query_params);
  } catch (PDOException $ex) {
    $response["error"] = true;
    $response["message"] = "Database Error1. Please Try Again!";
    die(json_encode($response));
  }

  $validated_info = false;
  $login_ok = false;
  $email = $_POST['email'];

  $row = $stmt->fetch();

  if (password_verify($_POST['password'],$row['encrypted_password'])) {
    $login_ok = true;
  }

  if ($row['created_at'] <= $row['exp_date']) {
    $response["error"] = true;
    $response["message"] = "Subscription ended!";
    die(json_encode($response));
  }

  if ($login_ok == true) {
    $response["error"] = false;
    $response["message"] = "Login successful!";
    $response["user"]["uid"] = $row["unique_id"];
    $response["user"]["name"] = $row["name"];
    $response["user"]["email"] = $row["email"];
    $response["user"]["is_active"] = $row["is_active"];
    $response["user"]["created_at"] = $row["created_at"];
    $response["user"]["exp_date"] = $row["exp_date"];
    die(json_encode($response));
  } else {
    $response["error"] = true;
    $response["message"] = "잘못된 로그인 정보!";
    die(json_encode($response));
  } 

} else {
  echo 'Nothing here';
}

?>

请帮助我们。对不起,我的英语不好。

1 个答案:

答案 0 :(得分:0)

使用date_format(php函数)比较日期,并将订阅到期日期与今天的日期进行比较。我不确定为什么要与created_at进行比较。

$ date_today = date(“ m / d / Y”); //使用您从数据库获取的日期格式,也使用时间戳Y-m-d H:i:s,以便比较时间戳而不只是日期

$ date_of_subscription = $ row ['exp_date'];

$ date_converted = date_format($ date_of_subscription,“ m / d / Y”);

如果($ date_today> $ date_converted){

回显“过期”;

}