Reports and \DatePeriod PHP in reports

Sometime i’m working with many various of reports generate by PHP + MySql, its not bad but MySql isn’t best engine to do it when axis x is defined as a time period, DateTime official php class and DateInterval helping a lot.

How to get in the easiest way weeks margin date in defined period, counting start from monday ?

For eg. I want get count reports for visiting page for last 4 weeks and i need do some calculations in php like change of the trend…

$begin = new \DateTime('now');
$begin = $begin->modify( '-4 week mondays' );

$end = new \DateTime('now');
$interval = new \DateInterval('P1W');

$dateRanges = new \DatePeriod($begin, $interval, $end);
$ranges = array();

foreach ($dateRanges as $i => $start) {
	$end = $start;
	$ranges[] = [
		'w' => $start->format('W'),
		'begin' => $start->format('Y-m-d') . ' 00:00:00',
		'end' => $end->modify('+6 day')->format('Y-m-d') . ' 23:59:59',
		'last' => (($weeks-1) == $i) ? true : false

and Voilà 🙂 we have array with week number, and margin days of every week to use generate reports (compatible with ISO 8601 week dates)