1

我正在使用以下代码行来获取我尝试为某些报告生成的日期,它似乎工作正常,除了在少数情况下,我不明白为什么会这样。

// 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)) .')';
4

1 回答 1

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'));
于 2012-11-19T12:52:01.167 回答