Created
April 4, 2015 04:08
-
-
Save akozlik/2093209326b40e615aba to your computer and use it in GitHub Desktop.
Loop through an array of objects with dates and group by week
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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