Skip to content

Instantly share code, notes, and snippets.

View plindsay's full-sized avatar

Phil Lindsay plindsay

View GitHub Profile
@plindsay
plindsay / addMonths.php
Created March 30, 2017 21:01
Loop through months, accounting for February
<?php
/*
based on this SO answer - http://stackoverflow.com/a/40877555/134823
*/
$addMonths = function ($date, $months) {
$orig_day = $date->format('d');
$date->modify('+'.$months.' months');
while ($date->format('d') < $orig_day && $date->format('d') < 5) {
@plindsay
plindsay / DateInterval
Last active March 30, 2017 19:54
DateInterval skips February?..
$begin = new \DateTime('-3 month');
$end = new \DateTime(date('Y-m-d'));
$end = $end->modify('+1 month');
$interval = \DateInterval::createFromDateString('+1 month');
$period = new \DatePeriod($begin, $interval, $end);
foreach($period as $dt) {
$start = $dt->format('Y-m').'-01';
echo $start."<br>";
}