我有一个脚本,它计算到某人下一个生日的天数(可能有更好的方法),但无论如何,如果某人的生日是3天前,我希望它给我362天,直到下一个生日,但它给了-3。没问题,再加上365天就可以得到正确的答案,但这根本不起作用,我搞不懂为什么…代码:

$resultcurdate = mysql_query("select curdate()",$db);

    $curdate = mysql_fetch_array($resultcurdate);

    $curdate1 = explode('-', $curdate[0]);
    $day1   = $curdate1[2];
    $month1 = $curdate1[1];
    $year1  = $curdate1[0];

    $birthdate = explode('-', $databack10[birthday]);
    $day   = $birthdate[2];
    $month = $birthdate[1];
    $year  = $birthdate[0];

    $year = $year1;

    if ($month1>$month){$year=$year+1;}

    $birthdate5 = $year."-".$month."-".$day;

    $resultdate = mysql_query("select datediff('$birthdate5', now())",$db);

    $databackdate = mysql_fetch_array($resultdate);

    if($databackdate < '0'){$databackdate = (365 + $databackdate); }

你知道为什么这样不行吗?
有没有办法用SQL查询来实现这一点?生日是日期字段类型。

最佳答案

select
date_field,
abs(if(right(curdate(),5) >= right(date_field,5),
datediff(curdate(),concat(year(curdate()+ interval 1 year),right(date_field,6))) ,
datediff(concat(year(curdate()),right(date_field,6)),curdate()))) as days
from table

关于php - 尝试计算下一个生日的天数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8377559/

10-15 18:40