doumubi6784 2012-11-19 11:56
浏览 4
已采纳

PHP复合日期格式错误

I am using the following lines of code to get the dates I am trying to generate for some reports and it seems to work fine except in a few instances and I can't see why that would be.

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

On this page I have some select menu controls for changing the period and year number and when it gets to certain periods I get some bizarre dates.

The periods are quite confusing but if anybody has any questions then feel free to ask.

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

As you can see, it seems to go insane for periods 1 and 2 for some reason and I don't understand why.

The parameters are passed in the following way: ?wA=1&yA=2012 and if those are not set it uses the current month and period.

If I was to guess then I'd say it might have something to do with leap years but I thought the code would be able to handle that automatically? Who knows, hopefully a few extra pairs of eyes will spot something stupid I've missed.

Code that echo's the dates

date("d/m/Y", strtotime($period_start)) . ' - ' . date("d/m/Y", strtotime($period_end)) .')';
  • 写回答

1条回答 默认 最新

  • doudeng2016 2012-11-19 12:52
    关注

    The error is from those 2 lines

    $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'));
    

    if the week number is 2 for example, adding a 6 to it, it would be 26 (week 26), so you have to add a zero to the left side for the single digit ones. Let's do that with 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'));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分