2017-08-03 184 views
1

我有一個包含項目的數組。每個項目都有一個標題和startdate。現在我想按周分組這些項目。如果我查看文檔,格式 - >('W') 星期星期一開始。我需要改變這個星期六。PHP數組與星期六星期六至星期五

 $items = [ 
     [ // friday 
      'title'  => 'test', 
      'startdate' => '2016-03-18', // first group 
     ], 
     [ // saturday 
      'title'  => 'test', 
      'startdate' => '2016-03-19', // second group 
     ], 
     [ // sunday 
      'title'  => 'test', 
      'startdate' => '2016-03-20', // second group 
     ], 
     [ // monday 
      'title'  => 'test', 
      'startdate' => '2016-03-21', // second group 
     ], 
     [ // saterday 
      'title'  => 'test', 
      'startdate' => '2016-03-26', // third group 
     ], 
    ]; 

我有這樣的事情,但它不能正常工作

 foreach($items as $item) { 
      // group by week 
      $startDate  = \DateTime::createFromFormat('Y-m-d', $item['startdate']); 
      $currectWeek = $startDate->format('W'); 
      $saturday  = 6; 
      $friday   = 5; 

      $test = clone $startDate; 

      if($currectWeek !== 5){ 
       $test->modify('last friday'); 
      } 

      if($currectWeek !== 6){ 
       $test->modify('next saturday'); 
      } 

      $a = $test->format('W'); 

      if(!isset($items_by_week[$a])){ 
       $items_by_week[$a] = []; 
      } 

      $items_by_week[$a][] = [ 
       'title' => $item['title'], 
       'date'  => $item['startdate'], 
      ]; 
     } 
+0

什麼不起作用?我看到您的$ items_by_week數組按週數分組。請讓我們知道您正在尋找示例輸入提供的示例輸出 –

+1

我使用以下解決方案解決它:https://stackoverflow.com/questions/13128854/php-datetime-class-change-first-day-of-the星期一到星期一#answer-13129157 – Bham

回答

0

試試下面的代碼

$startDate = \DateTime::createFromFormat('Y-m-d', $item['startdate']); 
$week = intval($startDate->format('W')); 
$day = intval($startDate->format('N')); 

$a = ($day < 6) ? $week-1 : $week; 
+0

我怎樣才能考慮不同年份 – Bham

+0

這取決於你想要算作第一週的新年開始的最少天數。在min = 4的情況下,我的代碼將工作,因爲在PHP中實現DateTime – Denis

0

分組是一週(其中一週開始在週六結束上週五是加工)。這是結果,使用關閉下面的代碼:

$byWeek = array(); 
foreach ($items as $item) { 
    $date = DateTime::createFromFormat('Y-m-d', $item['date']); 

    $firstDayOfWeek = 6; // Saturday 

    $difference = ($firstDayOfWeek - $date->format('N')); 
    if ($difference > 0) { $difference -= 7; } 
    $date->modify("$difference days"); 
    $week = $date->format('W'); 

    if(!isset($byWeek[$week])){ 
     $byWeek[$week] = []; 
    } 

    $byWeek[$week][] = $item; 
} 

// result 
     Array 
     (

     [31] => Array 
      (
       [0] => Array 
        (
         [title] => test 11 
         [date] => 2017-08-11 
        ) 

      ) 

     [32] => Array 
      (
       [0] => Array 
        (
         [title] => test 
         [date] => 2017-08-12 
        ) 

       [1] => Array 
        (
         [title] => test 222 
         [date] => 2017-08-12 
        ) 

       [2] => Array 
        (
         [title] => test 1 
         [date] => 2017-08-13 
        ) 

       [3] => Array 
        (
         [title] => test 2 
         [date] => 2017-08-14 
        ) 

      ) 

但現在我也想在一週內分組。所以需要如下輸出

Array 
(
    [32] => Array 
     (
      [0] => Array 
       (
        [0] => Array 
         (
          [title] => test 
          [date] => 2017-08-12 
         ) 

        [1] => Array 
         (
          [title] => test 222 
          [date] => 2017-08-12 
         ) 

       ) 

      [1] => Array 
       (
        [title] => test 1 
        [date] => 2017-08-13 
       ) 

     ) 

) 
相關問題