PHP:生日检查今天的日期
答
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'))
答
这个答案应该的工作,但是这取决于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!
}
答
从PHP 5.2起:
if (substr($dateFromDb, -5) === date_create()->format('m-d')) {
// Happy birthday!
}
答
<?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));
}
你也应该允许泰德的生日在2004年2月29日 - 2011年,他将迎来在02-28他的生日。 – 2010-10-06 14:41:59
尼斯认为蒂姆,这怎么可能呢? – Johnson 2010-10-06 14:50:56
谢谢蒂姆。我已经改变了答案。 – CristiC 2010-10-06 15:07:43