PHP Mysql:在表中查找过期日期并扩展它们

时间:2015-01-06 17:24:45

标签: php mysql date

PHP Mysql:在表中查找过期日期并扩展它们

嗨 - 我有一个php分类网站,我会运行一个命令,找到过期的广告并自动将日期增加XX天数。

这是结构

DATEBASE = "realestatedb"   
table names are 
"tt_44" to "tt_56" 

字段名称ExpireDate类型为日期

我已阅读.. PHP Select from MySQL where date field is 7 days in the future

但无法弄清楚

这会有效吗

SELECT * 
  FROM realestatedb
  WHERE status IN('','ExpireDate')
   AND expiry_date = DATE(NOW() + INTERVAL 7 DAY)

2 个答案:

答案 0 :(得分:0)

添加到日期的MySQL函数是DATE_ADD。您使用DATE_ADD(fieldname,INTERVAL 7 DAY)向该字段添加7天。

您想要将过期日期添加到过期日期。到期时,我认为你的意思是他们在某个约会之前。因此,假设在2015-01-01之前已过期。然后,您将使用:

UPDATE tt_44 SET ExpireDate = DATE_ADD(ExpireDate, INTERVAL 7 DAY) where ExpireDate<'2015-01-01';

这会在表tt_44

中为2015-01-01之前的每个日期增加7天

答案 1 :(得分:0)

我必须运行该命令几次以便更新所有这些命令你知道为什么会这样......

mysql> UPDATE tt_44 SET ExpireDate = DATE_ADD(ExpireDate, INTERVAL 365 DAY) where ExpireDate<'2015-01-01';
Query OK, 72 rows affected (0.01 sec)
Rows matched: 72  Changed: 72  Warnings: 0

mysql> UPDATE tt_44 SET ExpireDate = DATE_ADD(ExpireDate, INTERVAL 365 DAY) where ExpireDate<'2015-01-01';
Query OK, 15 rows affected (0.00 sec)
Rows matched: 15  Changed: 15  Warnings: 0

mysql> UPDATE tt_44 SET ExpireDate = DATE_ADD(ExpireDate, INTERVAL 365 DAY) where ExpireDate<'2015-01-01';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> UPDATE tt_44 SET ExpireDate = DATE_ADD(ExpireDate, INTERVAL 365 DAY) where ExpireDate<'2015-01-01';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> UPDATE tt_44 SET ExpireDate = DATE_ADD(ExpireDate, INTERVAL 365 DAY) where ExpireDate<'2015-01-01';
Query OK, 0 rows affected (0.01 sec)
Rows matched: 0  Changed: 0  Warnings: 0

mysql> UPDATE tt_44 SET ExpireDate = DATE_ADD(ExpireDate, INTERVAL 365 DAY) where ExpireDate<'2015-01-01';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0
相关问题