gpt4 book ai didi

php - 两个日期之间的 YEARWEEKS

转载 作者:可可西里 更新时间:2023-11-01 07:58:47 27 4
gpt4 key购买 nike

我想使用 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
)
*/

这些不再有效,因为我的日期可能在不同的年份。

拥有包含 future 10 年所有日期的数据库不是一种选择

当周 > 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;
}

最佳答案

您可以使用 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手册中的类(class)。

关于php - 两个日期之间的 YEARWEEKS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25601664/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com