如何通过指定的日期范围来判断季节


How to tell the season by specified range of dates

我将根据给定的日期指定季节。我有四个季节,按日期(而不是按月份)排列。所以我决定使用in_arrayrange(),但它什么也没显示。

这是我的代码:

$p1=strtotime("2013-12-13");
$p2=strtotime("2014-02-20");
$h1a=strtotime("2014-02-21");
$h1b=strtotime("2014-04-31");
$l1=strtotime("2013-05-01");
$l2=strtotime("2013-10-31");
$h2a=strtotime("2013-11-01");
$h2b=strtotime("2013-12-19");

$today=strtotime(date("Y-m-d"));
if(in_array($today, range($p1, $p2))){
    echo "peak";
}elseif(in_array($today, range($h1a, $h1b))){
    echo "hi1";
}elseif(in_array($today, range($l1, $l2))){
    echo "low";
}else(in_array($today, range($h2a, $h2b))){
    echo "h2";
}

你们能改进一下我的代码吗。

问候,

我现在有了自己的解决方案。感谢这些试验。该代码改编自:http://css-tricks.com/snippets/php/change-graphics-based-on-season/

<?
function current_season() {
       // Locate the icons
       $icons = array(
               "peak" => "peak season",
               "low" => "low season",
               "high1" => "high1 season",
               "high2" => "high2 season"
       );
       // What is today's date - number
       $day = date("z");
       //  Days of peak
       $peak_starts = date("z", strtotime("December 13"));
       $peak_ends   = date("z", strtotime("February 20"));
       //  Days of low
       $low_starts = date("z", strtotime("May 1"));
       $low_ends   = date("z", strtotime("October 31"));
       //  Days of high
       $high_starts = date("z", strtotime("February 21"));
       $high_ends   = date("z", strtotime("April 31"));
       //  If $day is between the days of peak, low, high, and winter
       if( $day >= $peak_starts && $day <= $peak_ends ) :
               $season = "peak";
       elseif( $day >= $low_starts && $day <= $low_ends ) :
               $season = "low";
       elseif( $day >= $high1_starts && $day <= $high1_ends ) :
               $season = "high";
       else :
               $season = "high2";
       endif;
       $image_path = $icons[$season];
       echo $image_path;
}
echo current_season();
?>

因为您的内存限制超出了范围。我只在射程内使用了2天http://codepad.viper-7.com/PLFObE它显示了大部分的输出。

您可以使用大于和小于来度量日期。

if($today >= $p1 && $today <= $p2){
    echo "peak";
}elseif($today >= $h1a && $today <= $h1b){
    echo "hi1";
}elseif($today >= $l1 && $today <= $l2){
    echo "low";
}else($today >= $h2a && $today <= $h2b){
    echo "h2";
}

编辑

编码板