【发布时间】:2010-10-06 14:38:18
【问题描述】:
我将用户的生日存储在 birthday 中,为 1999-02-26。
如何查看生日是否是今天?
if(date('m-d') == ..?
【问题讨论】:
标签: php
我将用户的生日存储在 birthday 中,为 1999-02-26。
如何查看生日是否是今天?
if(date('m-d') == ..?
【问题讨论】:
标签: php
这个答案应该有效,但这取决于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
/**
* @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';
}
【讨论】:
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'))
【讨论】:
date('y')%4,因为a。该规则并不完全正确,并且 b。输入该日期时应该验证某个日期是否有效。
PHP 5.2 以上:
if (substr($dateFromDb, -5) === date_create()->format('m-d')) {
// Happy birthday!
}
【讨论】: