我使用以下代码行来获取我尝试为某些报告生成的日期,它似乎工作正常,除了在少数情况下,我不明白为什么会这样。
// what week numbers belong to which period
$adminconfig_periods = array(
1=>array(1,2,3,4),
2=>array(5,6,7,8,9),
3=>array(10,11,12,13),
4=>array(14,15,16,17),
5=>array(18,19,20,21,22),
6=>array(23,24,25,26),
7=>array(27,28,29,30),
8=>array(31,32,33,34,35),
9=>array(36,37,38,39),
10=>array(40,41,42,43),
11=>array(44,45,46,47,48),
12=>array(49,50,51,52,53)
);
/**
* Get period no for week no
*
* @param string week the week
* @return int - the period no
*/
function getPeriodForWeek($week) {
global $adminconfig_periods;
$foundperiod = false;
$period = 1;
while(!$foundperiod) {
if(in_array($week, $adminconfig_periods[$period])) {
$foundperiod = true;
} else {
$period ++;
}
}
return $period;
}
$wA = $_GET['wA'];
$yA = $_GET['yA'];
$prev_period = '';
$next_period = '';
if (!isset($wA) || !isset($yA)) {
// period and year aren't set so do it for current period
// period starts on first Sunday of the first week in the period and ends on the last Saturday of the last week in the period
$week = date('W');
$period = getPeriodForWeek($week);
$wA = date('m');
$yA = date('Y');
}
else {
// period and year are set
// period starts on first Sunday of the first week in the period and ends on the last Saturday of the last week in the period
$period = $wA;
}
// get date of first Sunday of the first week in this period
$period_start_week = $adminconfig_periods[$period][0];
// get the Sunday of this week
$period_start = date('Y-m-d', strtotime($yA . 'W' . $period_start_week . '0'));
// get date of last Saturday of the last week in this period
$period_length = count($adminconfig_periods[$period]);
// array indexes start from 0 so we need to take one off this value
$last_element = $period_length - 1;
$period_end_week = $adminconfig_periods[$period][$last_element];
// get the Saturday of this week
$period_end = date('Y-m-d', strtotime($yA . 'W' . $period_end_week . '6'));
在这个页面上,我有一些用于更改期间和年份数的选择菜单控件,当它到达某些时间段时,我会得到一些奇怪的日期。
这些时期非常混乱,但如果有人有任何问题,请随时提出。
Period | What it Should Be | Actual Result
11 | 28/10/2012 - 01/12/2012 | 28/10/2012 - 01/12/2012
10 | 30/09/2012 - 27/10/2012 | 30/09/2012 - 27/10/2012
09 | 02/09/2012 - 29/09/2012 | 02/09/2012 - 29/09/2012
08 | 29/07/2012 - 01/09/2012 | 29/07/2012 - 01/09/2012
07 | 01/07/2012 - 28/07/2012 | 01/07/2012 - 28/07/2012
06 | 03/06/2012 - 30/06/2012 | 03/06/2012 - 30/06/2012
05 | 29/04/2012 - 02/06/2012 | 29/04/2012 - 02/06/2012
04 | 01/04/2012 - 28/04/2012 | 01/04/2012 - 28/04/2012
03 | 04/03/2012 - 31/03/2012 | 04/03/2012 - 31/03/2012
02 | 29/01/2012 - 03/02/2012 | 10/12/2012 - 01/01/1970
01 | 01/01/2012 - 28/01/2012 | 05/03/2012 - 12/11/2012
正如你所看到的,由于某些原因,似乎在第1和第2期出现疯狂,我不明白为什么。
参数以下列方式传递:?wA=1&yA=2012
如果未设置参数,则使用当前月份和期间。
如果我猜测那么我会说它可能与闰年有关但我认为代码能够自动处理吗?谁知道,希望一双额外的双眼会发现一些我错过的愚蠢。
回显日期的代码
date("d/m/Y", strtotime($period_start)) . ' - ' . date("d/m/Y", strtotime($period_end)) .')';
答案 0 :(得分:1)
错误来自那两行
$period_start = date('Y-m-d', strtotime($yA . 'W' . $period_start_week . '0'));
$period_end = date('Y-m-d', strtotime($yA . 'W' . $period_end_week . '6'));
如果周数 2 ,例如添加 6 ,则 26 (第26周),所以你必须在左侧为零位数添加零。让我们用str_pad():
来做到这一点$period_start = date('Y-m-d', strtotime($yA . 'W' . str_pad($period_start_week, 2, 0, STR_PAD_LEFT) . '0'));
$period_end = date('Y-m-d', strtotime($yA . 'W' . str_pad($period_end_week, 2, 0, STR_PAD_LEFT) . '6'));