Stack Exchange
Arrange an array by weeks
I have an array in which I have field called date and what I need is to separate all these arrays into weeks. Is it posible to do so? Here is my code:
function getWeeks($query){
    $postdate = $query['response']['posts']['date'];


    return $posts;
}

Here is part of my array:
Array ( [date] => 07/30/12 [message] => test [post_id] => 1 [impressions] => Array ( [0] => 9638 ) [consumptions] => Array ( [0] => 38 ) [storytellers] => Array ( [0] => 6 ) [engaged_users] => Array ( [0] => 31 ) [story_adds] => Array ( [0] => 6 ) [impressions_unique] => Array ( [0] => 4700 ) [comment] => Array ( [0] => 1 ) [like] => Array ( [0] => 5 ) [share] => Array ( [0] => 0 ) [virality] => Array ( [0] => 0 ) [lifetime] => Array ( [0] => 0 ) [affinity] => Array ( [0] => 0 ) )

Array ( [date] => 07/30/12 [message] => test2 [post_id] => 2 [impressions] => Array ( [0] => 10552 ) [consumptions] => Array ( [0] => 47 ) [storytellers] => Array ( [0] => 5 ) [engaged_users] => Array ( [0] => 44 ) [story_adds] => Array ( [0] => 5 ) [impressions_unique] => Array ( [0] => 4982 ) [comment] => Array ( [0] => 0 ) [like] => Array ( [0] => 4 ) [share] => Array ( [0] => 1 ) [virality] => Array ( [0] => 0 ) [lifetime] => Array ( [0] => 0 ) [affinity] => Array ( [0] => 0 ) ) 


date('W', strtotime('07/30/12')) may help you. http://php.net/date

This will loop through each of your post items and group them together if they are in the same week.
View an Example
<?php

$posts = array(
  array('date' => '7/30/10', 'title' => 'july post'),
  array('date' => '7/19/10', 'title' => 'another post in july'),
  array('date' => '7/22/10', 'title' => 'sup, this will be with another post')
);

$grouped_posts = array();

foreach( $posts as $post ) {
  // @see http://us2.php.net/manual/en/function.date.php
  $week = date('W', strtotime($post['date']));

  // create new empty array if it hasn't been created yet
  if( !isset($grouped_posts[$week]) ) {
    $grouped_posts[$week] = array();
  }

  // append the post to the array
  $grouped_posts[$week][] = $post;
}

print_r($grouped_posts);


$week = date('Y W', strtotime($line['0'])); if need sort by weeks and years, becouse now 2015 01 01 and 2016 01 01 in same array :)

Hello, is it possible to create a key for each week?
Example: [0] - 2019-02-26 [1] - 2019-02-27
