我有一个有历史数据的数据库,我想通过日期按季节分类。
例如:如果第一季始于2014-01-01 - 2014-04-30,第二季为2014-05-01 - 2014-08-31,第三季为2014-09-01 - 2014-12-31,则第4、5和6季为2015-01- 2015-04-30、2015-05-01 - 2015-08-31、2015-09-01 - 2015-12-31。
这实际上只是从2014年开始的一年的增长。
我想做一些简单的事情,比如使用$_ get的查询字符串获得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 />";
?>我需要确切的日期
发布于 2015-07-07 22:08:29
从“季节编号”到“季节”,您可以使用模块化计算:$season = $theseasonnumber % 4
如果您想知道某个季节的日期范围,那么开始月份将以$endmonth = $season * 3结束,从$startmonth = $endmonth - 2开始。
从这里可以看到一些逻辑,并使用PHP中的date函数。
基于已编辑问题的编辑i使您成为一个工作示例函数
<?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>';
?>https://stackoverflow.com/questions/31280043
复制相似问题