如何判断假期是否已过去(每年)


How do I tell if a holiday has passed (every year)?

假设我想做一个网站来检查我的生日是否已经过去了,我可以很容易地做到

    <?php
if( strtotime("2012-10-21") < time() ){
    echo "Birthday has passed";
    } else {
        echo "Birthday hasn't passed";
    } 
?>

但这只能占一年,而不是未来的每一年。有人知道如何做到这一点吗?

<?php
    if (strtotime("21 October")<time()) {
        echo "Birthday has passed";
    } else {
        echo "Birthday hasn't passed";
    } 
?>

只需将 10 月 21 日传递到 strtotime() 年,就会自动返回当前年份的 UNIX 时间戳。

使用

mktime ,只需将 year 参数留空即可使用当前年份。

if (mktime(1, 1, 1, 10, 21) < time()) {
    ...
}

http://php.net/manual/en/function.mktime.php

PHP 手册:mktime

if(mktime(23, 59, 59, $birthdayDate, $birthdayMonth) < time())
{
    //Birthday has passed
}

编辑:我使用了23,59,59,这样它就不会说生日已经过去了,而它仍然是生日