我希望在两个给定日期之间使用MySQL / PHP获得一系列的一周。
到目前为止,我过去常常这样做,因为日期不会在不同的年份。
// $yw_min = yearweek of the min date
// $yw_max = yearweek of the max date
$yearweeks = array();
for ($i = $yw_min; $i <= $yw_max; $i++) {
$yearweeks[$i] = "W " . substr($i, 4, 6);
}
/*
yearweeks : Array (
[201501] => W 01
[201502] => W 02
[201503] => W 03
[201504] => W 04
[201505] => W 05
)
*/
这些不再适用,因为我的约会时间可能不同。
在未来10年内建立一个包含所有日期的数据库不是一个选项。
按周增加年份&gt; 52也是不可想象的,因为我不知道这一年是否包含51周,52周或53周。
编辑:这是故事的全部背景
我必须构建一周基于数据的图表。最初,我只把年份和季度作为“约会”。
我的功能是给我一周(年+季):
function getYearweeks($year, $quarter) {
$min = ($quarter - 1) * 3 +1;
$max = $quarter * 3 + 1;
$dmin = $year . "-" . (($min < 10)?"0" . $min:$min) . "-01";
$dmax = (($max > 12)?($year + 1) . "-01-01":$year . "-" . (($max < 10)?"0" . $max:$max) . "-01");
$q = "SELECT YEARWEEK('" . $dmin . "',1) AS yw_min,
YEARWEEK(DATE_SUB('" . $dmax . "', INTERVAL 1 DAY),1) AS yw_max";
$r = mysql_query($q) or die (mysql_error()."<br />".$q."<br />");
$d = mysql_fetch_assoc($r);
$yw_min = $d['yw_min'];
$yw_max = $d['yw_max'];
$yearweeks = array();
for ($i = $yw_min; $i <= $yw_max; $i++) {
$yearweeks[$i] = "W " . substr($i, 4, 6);
}
return $yearweeks;
}
问题是,现在,客户想要“滑动”;我在我的函数上添加了一个参数“interval”,这是一个星期来添加(或子)到原始日期(年+季度):
function getYearweeks($year, $quarter, $interval=0) {
$min = ($quarter - 1) * 3 +1;
$max = $quarter * 3 + 1;
$dmin = $year . "-" . (($min < 10)?"0" . $min:$min) . "-01";
$dmax = (($max > 12)?($year + 1) . "-01-01":$year . "-" . (($max < 10)?"0" . $max:$max) . "-01");
$q = "SELECT YEARWEEK(DATE_ADD('" . $dmin . "', INTERVAL " . $interval . " WEEK),1) AS yw_min,
YEARWEEK(DATE_SUB(DATE_ADD('" . $dmax . "', INTERVAL " . $interval . " WEEK), INTERVAL 1 DAY),1) AS yw_max";
$r = mysql_query($q) or die (mysql_error()."<br />".$q."<br />");
$d = mysql_fetch_assoc($r);
$yw_min = $d['yw_min'];
$yw_max = $d['yw_max'];
// mess up when we are between two different years
$yearweeks = array();
for ($i = $yw_min; $i <= $yw_max; $i++) {
$yearweeks[$i] = "W " . substr($i, 4, 6);
}
return $yearweeks;
}
答案 0 :(得分:5)
您可以使用DatePeriod
类在PHP中执行此操作。在DateTime
创建一个对象,DateInterval
也为DatePeriod
提供两个日期(分别为开头和结尾)。
示例:
$dates = array();
$begin = new DateTime('2014-09-01'); // beginning then from
$end = new DateTime('2015-02-02'); // end
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
foreach($daterange as $date) {
$dates[$date->format('o W')] = 'Week ' . $date->format('W'); // iso date
}
echo '<pre>';
print_r($dates);
应输出:
Array
(
[2014 36] => Week 36
[2014 37] => Week 37
[2014 38] => Week 38
[2014 39] => Week 39
[2014 40] => Week 40
[2014 41] => Week 41
[2014 42] => Week 42
[2014 43] => Week 43
[2014 44] => Week 44
[2014 45] => Week 45
[2014 46] => Week 46
[2014 47] => Week 47
[2014 48] => Week 48
[2014 49] => Week 49
[2014 50] => Week 50
[2014 51] => Week 51
[2014 52] => Week 52
[2014 01] => Week 01
[2015 01] => Week 01
[2015 02] => Week 02
[2015 03] => Week 03
[2015 04] => Week 04
[2015 05] => Week 05
[2015 06] => Week 06
)
有关详细信息,请访问手册中的DatePeriod
课程。