如何使用php-mysql在1个月后自动将数据库表中的状态从0更改为3

时间:2018-12-05 07:30:19

标签: php mysql

我正在尝试在我的网站中添加新功能,即我想确定1个月的期限,在该期限到期后,这意味着the status of that user will change from 0 to 3.

我的表中有很多字段,包括

  1. id
  2. 状态
  3. 今天。

今天,date_date存储了注册的时间和日期,默认状态为0。

使用php-mysql是否可能?如果可能的话,请帮帮我。 预先感谢。

3 个答案:

答案 0 :(得分:1)

您应该采用另一种方法来实现这一目标。

您已经在数据库权限中拥有注册日期,因此,当用户在登录表单中输入有效的凭证时,请将当前日期与注册日期进行比较,如果该差异超过30天,则使用以下命令更新status列: 0,并且当时也不允许该用户进入。

如何计算php中两个日期之间的天数:

$now = time(); // or your date as well
$your_date = strtotime("2010-01-01");
$datediff = $now - $your_date;

echo round($datediff / (60 * 60 * 24));  // This will be number of days. Store it in a variable and set a condition on it.

答案 1 :(得分:0)

如果您有用于处理登录POST数据的页面,则构建您的sql查询以获取包含用户注册时间的列。 以$time_of_reg作为用户的注册时间。 无论您使用什么时间戳,都将其转换为UNIX 使用strtotime()函数的时间戳记。

现在将用户的注册时间增加30天$time_of_reg_plus_30 = strtotime('+30 Days',$time_of_reg)

如果当前时间大于或等于30天+用户注册时间,请使用if条件执行更新查询。这样,只有在用户注册30天或更长时间之后,您才能对status列执行更新查询。

if(time()>=$time_of_reg_plus_30)
{ //perform sql update query}

(在UNIX时间戳记中具有这些值,因此比较起来更容易)

<?//perform sql query to fetch user's time of registration, '$time_of_reg' in this case.

 $time_of_reg = strtotime($time_of_reg);  //Convert to UNIX timestamp
 $time_of_reg_plus_30 = strtotime('+30 Days',$time_of_reg); // Add 30 Days To Registration Time

if(time()>=$time_of_reg_plus_30)
{ //perform sql update query if user has been registered for 30 Days or More
}

请记住将其放置在处理您的登录POST数据的页面中,这是一种无需cron作业即可实现目标的好方法,因为每当用户尝试登录时都会更新用户的状态。

答案 2 :(得分:0)

Cron工作是最好的解决方案。认为您不想在气球上贴上大提琴胶带。

如果您要按照以下步骤操作:

  1. 创建一个PHP脚本并保存在文件中,例如myCron.php

  2. 如果您的Linux服务器带有Centos,请使用bash How to create a cron job using Bash automatically without the interactive editor?

  3. 创建一个调度程序脚本。
  4. 如果您有cPanel转到cron作业,请设置文件路径和运行时间。