id birth
-----------------------------
1 1079280000 (2004-03-15)
2 987264000 (2001-04-15)
3 1042560000 (2003-01-15)
如何为sql转换为birth
时间戳为1300118400
(2011-03-15);
<?php
$now = strtotime('2011-03-04');// today's timestamp
$sql = "SELECT * FROM member
WHERE birth(1079280000 convert to 1300118400) > $now";
?>
Results
id birth
---------------
1 1079280000 (2004-03-15)
2 987264000 (2001-04-15)
我需要将birth
转换为当前年份的生日timestamp
。
答案 0 :(得分:2)
无子选择解决方案:
SELECT
id,
UNIX_TIMESTAMP(
FROM_UNIXTIME(birth) +
INTERVAL (YEAR(CURDATE()) - YEAR(FROM_UNIXTIME(birth))) YEAR
) AS birthday
FROM member
WHERE NOW() < FROM_UNIXTIME(birth) +
INTERVAL (YEAR(CURDATE()) - YEAR(FROM_UNIXTIME(birth))) YEAR
不确定MySQL如何使用派生表(ab),但上述查询可能更具可读性,如果重写如下:
/* Stage 3. Filtering out past birthdays and
converting datetime dates to unix timestamps. */
SELECT
id,
UNIX_TIMESTAMP(birthday_date) AS birthday
FROM (
/* Stage 2. Getting this year birthdays */
SELECT
id,
birth_date + INTERVAL (YEAR(CURDATE()) - YEAR(birth_date)) AS YEAR birthday_date
FROM (
/* Stage 1. Converting unix timestamps to datetime values. */
SELECT
id,
FROM_UNIXTIME(birth) AS birth_date
FROM member
) m
) m
WHERE NOW() < birthday_date
答案 1 :(得分:1)
这已在下面的帖子中解释
答案 2 :(得分:0)
SQL有一个DATEPART函数(http://msdn.microsoft.com/en-us/library/ms174420.aspx),您可以使用它来获取出生日期的日期和月份,并在比较中使用它们在PHP中获得今天的日期和月份。可能有更好的方法,但这种方式至少应该起作用。 :)
这当然假设您的日期存储为数据库中的日期而不是整数。 :)
答案 3 :(得分:0)
如果我理解,如果是mysql:
SELECT day(FROM_UNIXTIME(birth)) as birthday
FROM member
WHERE birth > $now