2010-10-06 167 views

回答

5
if(date('m-d') == substr($birthday,5,5)) 

要添加什麼蒂姆說:

if(date('m-d') == substr($birthday,5,5) or (date('y')%4 <> 0 and substr($birthday,5,5)=='02-29' and date('m-d')=='02-28')) 
+5

你也應該允許泰德的生日在2004年2月29日 - 2011年,他將迎來在02-28他的生日。 – 2010-10-06 14:41:59

+0

尼斯認爲蒂姆,這怎麼可能呢? – Johnson 2010-10-06 14:50:56

+0

謝謝蒂姆。我已經改變了答案。 – CristiC 2010-10-06 15:07:43

10

這個答案應該的工作,但是這取決於strtotime能找出你的數據庫的日期格式:

$birthDate = '1999-02-26'; // Read this from the DB instead 
$time = strtotime($birthDate); 
if(date('m-d') == date('m-d', $time)) { 
    // They're the same! 
} 
+0

_Should_在這個複雜的世界裏是不夠的。這些日期和時間計算總是傾向於在不同的邊緣和事件中存在隱藏的困難。 2月29日出生的人生日很困難。而檢查閏年是複雜的。這值得一些現成的解決方案。 – Gherman 2014-09-29 11:06:05

+0

@德國好吧,strtotime是由第三方寫的......順便說一句,我注意到當我寫這個時,我意外地鏈接到了strptime的手冊頁......這是一個不同的功能。順便說一句,因爲這個答案是寫的,PHP有DateTime對象和相關的功能添加到它。 – Powerlord 2014-09-29 13:43:35

0

從PHP 5.2起:

if (substr($dateFromDb, -5) === date_create()->format('m-d')) { 
    // Happy birthday! 
} 
2
<?php 
/** 
* @param string $birthday Y-m-d 
* @param int $now 
* @return bool 
*/ 
function birthdayToday($birthday, $now = null) { 
    $birthday = substr($birthday, -5); 
    if ($now === null) { 
     $now = time(); 
    } 
    $today = date('m-d', $now); 
    return $birthday == $today || $birthday == '02-29' && $today == '02-28' && !checkdate(2, 29, date('Y', $now)); 
}