php - 如何检查某个日期是否在某个季节(无论当前年份)?

时间:2014-02-05 12:41:52

标签: php date range

旺季: 4月28日 - 9月30日, 12月27日 - 1月3日

淡季: 10月1日 - 12月26日, 1月4日 - 4月27日


我给出了一个检查日期:2014-02-18我希望在哪个季节包括它时为TRUE或FALSE。无论当年如何,该怎么做?

3 个答案:

答案 0 :(得分:2)

尝试使用简单的日期比较:

function is_high_season($date) {
    $md = gmdate('m-d', strtotime($date));
    return 
        ('03-28' <= $md && $md <= '09-30') || 
        ('12-27' <= $md && $md <= '12-31') || 
        ('01-01' <= $md && $md <= '01-03');
}

demo

答案 1 :(得分:0)

$day = $d->format('j');
$month = $d->format('n');

if($month == 1 && $day <= 3) {
    return 'high';
} elseif $month < 4 || ($month == 4 && $day < 28)) {
    return 'low';
} elseif($month == 4 && $day >= 28) {
    return 'high';
} elseif($month < 10) {
    return 'high';
} elseif($month < 12 || ($month == 12 && $day < 27)) {
    return 'low';
} elseif($month == 12 && $day >= 27) {
    return 'high';
}

答案 2 :(得分:-1)

 $Date = date('Y-m-d');
    $Date=date('Y-m-d', strtotime($Date));;
    //echo $Date; // echos today! 
    $hiDateBegin = date('Y-m-d', strtotime("28/04/2014"));
    $hiDateEnd = date('Y-m-d', strtotime("30/09/2014"));
    $hiDateBegin2 = date('Y-m-d', strtotime("27/12/2014"));
    $hiDateEnd2 = date('Y-m-d', strtotime("03/01/2015"));

    $lowDateBegin = date('Y-m-d', strtotime("01/10/2014"));
    $lowDateEnd = date('Y-m-d', strtotime("26/12/2014"));
    $lowDateBegin2 = date('Y-m-d', strtotime("04/01/2015"));
    $lowDateEnd2 = date('Y-m-d', strtotime("27/04/2015"));

    if (($Date > $hiDateBegin) && ($Date < $hiDateEnd))
    {
      echo "is Hi Season";
    }
    if (($Date > $hiDateBegin2) && ($Date < $hiDateEnd2))
    {
      echo "is Hi Season";
    }
if (($Date > $lowDateBegin) && ($Date < $lowDateEnd))
    {
      echo "is Low Season";
    }
    if (($Date > $lowDateBegin2) && ($Date < $lowDateEnd2))
    {
      echo "is Low Season";
    }
    else
    {
    echo "Date doesn't fall in a season!";  
    }
相关问题