I have an array of random dates (not coming from MySQL). I need to group them by the week as Week1, Week2, and so on upto Week5.
What I have is this:
function getWeekOfMonth(DateTime $date) { $firstDayOfMonth = new DateTime($date->format('Y-m-1')); return ceil(($firstDayOfMonth->format('N') + $date->format('j') - 1) / 7); }
Goendg solution does not work for 2016-10-31.