php 以编程方式获取季节
php getting seasons programmatically
我有一个包含历史数据的数据库,我想通过日期按季节对它们进行分类
例如。如果第 1 季开始于 2014-01-01 - 2014-04-30,第 2 季开始于 2014-05-01 - 2014-08-31,第 3 季开始于 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 number' 到您可以使用模计算的季节:
$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>';
?>
我有一个包含历史数据的数据库,我想通过日期按季节对它们进行分类
例如。如果第 1 季开始于 2014-01-01 - 2014-04-30,第 2 季开始于 2014-05-01 - 2014-08-31,第 3 季开始于 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 number' 到您可以使用模计算的季节:
$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>';
?>