PHP以编程方式获取季节


php 5.3 - php getting seasons programmatically

我有一个具有历史数据的数据库,我想通过日期按季节对它们进行排序

。假设第一季开始时间为2014-01-01 - 2014-04-30,第二季开始时间为2014-05-01 - 2014-08-31,第三季开始时间为2014-09-01 - 2014-12-31,那么第4、5、6季将分别为2015-01-01 - 2015-04-30、2015-05-01 - 2015-08-31和2015-09-01 - 2015-12-31。

实际上只是从2014年开始上升。

我想做一些简单的事情,如使用查询字符串$_GET['season']来获得season=2,并以编程方式知道查找基于开始年份的2014-05-01 - 2014-08-31。

最好的方法是什么?

到目前为止我有这个代码

 <?
      $season = $_GET['season'];
      $endmonth = $season * 4;
      $startmonth = $endmonth - 4;
      echo "startmonth: " . $startmonth . "<br />";
      echo date("Y-m-d H:i:s", strtotime("+" . $startmonth . " months", strtotime('2005-01-01 00:00:00'))) . "<br />";
      echo date("Y-m-d H:i:s", strtotime("+" . $endmonth . " months", strtotime('2005-01-01 00:00:00'))) . "<br />";
 ?>

我需要准确的日期

从'季节数'到季节,你可以使用模计算:$season = $theseasonnumber % 4

如果你想知道一个季节的日期范围,那么开始月份将在$endmonth = $season * 3结束,$startmonth = $endmonth - 2开始

从那里只是一些逻辑和玩弄从PHP的日期函数。

基于编辑的问题进行编辑我为您创建了一个工作示例函数

<?php
function season_to_dates($season, $startyear = 2014)
{
    $year = floor($season / 4); // this are years on top of the startyear
    $actual_season = $season % 4; // returns the actual season
    if($actual_season == 0) $actual_season = 4; // little trick
    $endmonth = $actual_season * 3; 
    $startmonth = $endmonth - 2; 
    return array(
        'season' => $actual_season,
        'start' => date("Y-m-d", mktime(0,0,0,$startmonth,1,$year+$startyear)),
        'end'   => date("Y-m-t", mktime(0,0,0,$endmonth,1,$year+$startyear))
    );
}

if(!isset($_GET['season'])) $season = rand(1,40);
else $season = $_GET['season'];
echo 'Result for season ' . $season . '<pre>';
print_r(season_to_dates($season));
echo '</pre>';

?>