我有用户的生日存在 birthday
中,格式为 1999-02-26。
如何检查今天是否是生日?
if(date('m-d') == ..?
我有用户的生日存在 birthday
中,格式为 1999-02-26。
如何检查今天是否是生日?
if(date('m-d') == ..?
这个答案应该能够工作,但它取决于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!
}
if(date('m-d') == substr($birthday,5,5))
To add what Tim said:
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'))
date('y')%4
,因为 a.这个规则并不完全正确,b.是否验证某个特定日期是否有效应该在输入该日期时完成。 - Tim Pietzcker<?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));
}
$birthday = new DateTime("05-28-2020");
$today = new DateTime(date("Y-m-d"));
if ($birthday->format("m-d") == $today->format("m-d")) {
echo 'Today is your birthday';
} else {
echo 'Today is not your birthday';
}
从 PHP 5.2 开始:
if (substr($dateFromDb, -5) === date_create()->format('m-d')) {
// Happy birthday!
}