简体   繁体   中英

php - how to check if a date is in a given season (regardless of the current year)?

High Season: April 28 - September 30, December 27 - January 3

Low Season: October 1 - December 26, January 4 - April 27


I gave a date to check: 2014-02-18 and I want to have TRUE or FALSE in case of which season includes it. How to do it regardless of the current year?

Try with simple date comparing:

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

$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';
}
 $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!";  
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM