Skip to content

Instantly share code, notes, and snippets.

@akozlik
Created April 4, 2015 04:08
Show Gist options
  • Save akozlik/2093209326b40e615aba to your computer and use it in GitHub Desktop.
Save akozlik/2093209326b40e615aba to your computer and use it in GitHub Desktop.
Loop through an array of objects with dates and group by week
$weeks = array();
$index = 0;
while (count($tmp_logs) > 0) {
$log = array_shift($tmp_logs);
$date = $log['date'];
$sunday = strtotime('last Sunday', $date);
$saturday = strtotime('Saturday 11:59pm', $date);
$done = false;
$weeks[$index][] = $log;
while (!$done) {
if (count($tmp_logs) == 0) { $done = true; }
else {
$next_log = $tmp_logs[0];
$next_date = $next_log['date'];
if ($next_date >= $sunday && $next_date <= $saturday) {
$weeks[$index][] = array_shift($tmp_logs);
} else {
$done = true;
}
}
}
$index++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment