给两个日期(或两个月)检测我们在PHP的哪个季节

时间:2011-10-28 10:13:07

标签: php

我想知道是否有一些简单的方法来确定季节:春季,夏季,秋季或冬季。我必须制作一份“简历”,如果一段时间的工作大致适合一个季节(例如,不完全相同,但错误时间为+/- 10天),我就会回复春季,夏季,秋季或冬季。

像:

  • 输入:25/06 / 2010,30 / 09/2010
  • 输出:2010年夏季

(西班牙夏季是7月21日至9月20日)

  • 输入:02/02 / 2009,30 / 04/2010
  • 输出:2009-2010

知道怎么做吗?

3 个答案:

答案 0 :(得分:2)

看起来这个人已经写了这个函数:http://biostall.com/get-the-current-season-using-php

即使有半球支持!

但这应该可以解决问题:

<?php
    function getSeason($date) {
        $season_names = array('Winter', 'Spring', 'Summer', 'Fall');  
        if (strtotime($date) < strtotime($date_year.'-03-21') || strtotime($date) >= strtotime($date_year.'-12-21')) {  
            return $season_names[0]; // Must be in Winter
        } elseif (strtotime($date) >= strtotime($date_year.'-09-23')) {  
            return $season_names[3]; // Must be in Fall  
        } elseif (strtotime($date) >= strtotime($date_year.'-06-21')) {  
            return $season_names[2]; // Must be in Summer  
        } elseif (strtotime($date) >= strtotime($date_year.'-03-21')) {  
            return $season_names[1]; // Must be in Spring  
        }  
    }

答案 1 :(得分:2)

当你正在寻找一个时期的一个季节我很快写了这个功能,它可以改进,可能有一些错误,但你有一些地方可以开始。

function season($period) 
{
    $seasons    = array(
        'spring'    => array('March 21'     , 'June 20'),
        'summer'    => array('June 21'      , 'September 22'),
        'fall'      => array('September 23' , 'December 20'),
        'winter'    => array('December 21'  , 'March 20')
    );

    $seasonsYear = array();

    $start      = strtotime($period[0]);
    $end        = strtotime($period[1]);

    $seasonsYear[date('Y', $start)] = array();

    if (key(current($seasonsYear)) != date('Y', $end))
        $seasonsYear[date('Y', $end)] = array();

    foreach ($seasonsYear as $year => &$seasonYear)
        foreach ($seasons as $season => $period)
            $seasonYear[$season] = array(strtotime($period[0].' '.$year), strtotime($period[1].' '.($season != 'winter' ? $year : ($year+1))));

    foreach ($seasonsYear as $year => &$seasons) {
        foreach ($seasons as $season => &$period) {
            if ($start >= $period[0] && $end <= $period[1])
                return ucFirst($season).' '.$year;

            if ($start >= $period[0] && $start <= $period[1]) {
                if (date('Y', $end) != $year) 
                    $seasons = $seasonsYear[date('Y', $end)];   
                    $year = date('Y', $end);

                $nextSeason = key($seasons);
                $nextPeriod = current($seasons);                
                do {                    
                    $findNext   = ($end >= $nextPeriod[0] && $end <= $nextPeriod[1]);

                    $nextSeason = key($seasons);
                    $nextPeriod = current($seasons);
                } while ($findNext = False);

                $diffCurr   = $period[1]-$start;
                $diffNext   = $end-$nextPeriod[0];

                if ($diffCurr > $diffNext)
                    return ucFirst($season).' '.$year;
                else {
                    return ucFirst($nextSeason).' '.$year;
                }
            }
        }
    }
}

echo season(array('07/20/2010', '08/20/2010'));
echo "\n";
echo season(array('06/25/2010', '09/30/2010'));
echo "\n";
echo season(array('08/25/2010', '11/30/2010'));
echo "\n";
echo season(array('12/21/2010', '01/01/2011'));
echo "\n";
echo season(array('12/21/2010', '03/25/2011'));

结果:

/*
Summer 2010
Summer 2010
Fall 2010
Winter 2010
Winter 2011
*/

除了你想要的“季节溢出”之外:

if (date('Y', $end) != $year) 
    return $year.'-'.date('Y', $end);

而不是:

if (date('Y', $end) != $year) 
    $seasons = $seasonsYear[date('Y', $end)];   
    $year = date('Y', $end);

注意:冬天来了。

答案 2 :(得分:1)

以下是您需要做的概述:

  • 确定你的季节性界限。如果您打算在国际范围内这样做,这是一项很重要的大型工作!

  • 当提供日期范围时,首先确定每个季节中该范围的确切天数。

您希望输出如下:

 Range | Days | complete?
  Su10 |  12  |     0
   A10 |  90  |     1
   W10 |  02  |     0
  • 完成此操作后,您需要确定单个季节是否“完整”,如果整个季节在10天内完成,则应为1。如果是,请选择那个季节。如果有更多内容,或者没有,请返回false