PHP

HOW TO CREATE A DATE RANGE IN PHP

(simple examples)

START & END DATE $start = new DateTime("2020-01-01"); $end = new DateTime("2020-12-12");

DEFINE INTERVAL - DAY/MONTH/YEAR $interval = new DateInterval("P1D"); $interval = new DateInterval("P1M"); $interval = new DateInterval("P1Y");

DATE-TIME OBJECT

01

DATE RANGE & LOOP $range = new DatePeriod   ($start, $interval, $end); foreach ($range as $d)   { echo $d->format("Y-m-d"); }

START & END DATE $start = strtotime("2020-01-01"); $end = strtotime("2020-12-12");

DATE RANGE - 1 DAY INTERVAL for (let $i=$start; $i<=$end; $i+=86400)   { echo date("Y-m-d", $i); }

UNIX TIMESTAMP RANGE

02

DATE RANGE - 1 MONTH INTERVAL $i = $start; while ($i < $end) {   $idate = date("Y-m-d", $i);   $i = strtotime($idate . "+1 month"); }