2012-11-19 54 views
1

我正在使用下面的代碼行來獲取我想要爲某些報告生成的日期,並且它似乎能夠正常工作,除非在少數情況下,我看不出爲什麼會這樣。PHP複合日期格式錯誤

// 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

哪裏是實際生成輸出的代碼?你發佈的代碼產生'Y-m-d',但你的輸出使用'd/m/Y'。 – DCoder

+0

對不起,它是'date(「d/m/Y」,strtotime($ period_start))。 ' - '。日期(「d/m/Y」,strtotime($ period_end))。')';' - 我會更新問題 – martincarlin87

+1

哦,現在我明白了。在第1周,您的日期字符串看起來像「2012W10」和「2012W16」。日期解析器分別將其視爲「第10周」和「第16周」。根據[文檔](http://us.php.net/manual/en/datetime.formats.compound.php),在週數和日數之間添加短劃線可能會有所幫助。 – DCoder

回答

1

的錯誤是來自那些2線

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

如果週數爲例如,加入到它,這將是(第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')); 
+1

那個伎倆,謝謝! – martincarlin87