计算生日事件的mysql中的减去日期

时间:2013-09-05 08:19:00

标签: php mysql events

我创建了一个计算生日的事件,例如:

生日日期= 1990-09-07 现在日期= 2013-09-05

我的疑问:

SELECT id FROM user WHERE ( birthday - NOW() ) <= 7

此查询错误

4 个答案:

答案 0 :(得分:3)

我认为DAYOFYEAR功能更适合你:

mysql> SELECT DAYOFYEAR('2000-09-07') - DAYOFYEAR(now()) AS diff;
+------+
| diff |
+------+
|    3 |
+------+

今天是2013-09-05,它给了3天。现在你可以撰写条件了。请注意闰年的1天。

答案 1 :(得分:2)

你说它是生日,所以你必须考虑月份和日期,因为每个月都可以重复日期..

请尝试下面的内容,年份差异

SELECT
 (YEAR(birthdate) - YEAR(NOW())) AS yeardifference
FROM
 table
WHERE
  MONTH(birthdate) = MONTH(NOW())
AND
  DATEDIFF(birthdate, NOW()) <= 7

日差

SELECT
  DATEDIFF(birthdate, NOW())
FROM
  table
WHERE
  MONTH(birthdate) = MONTH(NOW())
AND
  DATEDIFF(birthdate, NOW()) <= 7

答案 2 :(得分:1)

我认为您的查询应该类似于

SELECT id 
FROM user 
WHERE (  DAYOFYEAR(birthday) - DAYOFYEAR(NOW())  <= 7) 

答案 3 :(得分:1)

Dayofyear功能非常有用,只需将其与您的要求放在上下文中

SELECT id FROM user WHERE (DAYOFYEAR(birthday) - DAYOFYEAR(NOW()))  <= 7