$current_season_expiry
值为"2016-07-23"
,但日期尚未过去,无论日期显示为“已过期”。我将$ current_season_expiry与今天的日期进行比较。
echo $current_season_expiry;
if( $current_season_expiry >= date("Ymd") ) {
echo "is not expired";
}else{
echo "expired";
}
答案 0 :(得分:2)
使用时间戳进行测试:
$timestamp_current_season_expiry = strtotime('2016-07-23');
$timestamp_date = strtotime(date('Y-m-d'));
if( $timestamp_current_season_expiry >= $timestamp_date ) {
echo "is not expired";
}else{
echo "expired";
}
答案 1 :(得分:2)
对于初学者,您需要在对date()的调用中添加破折号:
变化:
date("Ymd")
要:
date("Y-m-d")
原文会产生一串“20160624”而不是“2016-06-24”,这会导致任何比较。
还有未来出现时区问题的可能性,您可以通过确保使用UTC完成所有比较来缓解这些问题:
echo $current_season_expiry_utc; // Make sure this is in UTC
if( $current_season_expiry_utc >= gmDate("Y-m-d") ) {
echo "is not expired";
}else{
echo "expired";
}
...或使用时间戳:
echo $current_season_expiry_stamp; // Make sure this is a timestamp
if( $current_season_expiry_stamp >= time() ) {
echo "is not expired";
}else{
echo "expired";
}