How To Create Date Range In PHP (Simple Examples)

Welcome to a quick tutorial on how to create a date range in PHP. Need to loop through a range of dates, or generate date intervals?

There are 2 common ways to create a range of dates in PHP:

  1. Using DateTime,DateInterval, and DatePeriod objects.
    • $start = new DateTime("2020-01-01");
    • $end = new DateTime("2020-02-02");
    • $interval = new DateInterval("P1D");
    • $range = new DatePeriod($start, $interval, $end);
    • foreach ($range as $d) { echo $d->format("Y-m-d"); }
  2. Using Unix Timestamps.
    • $start = strtotime("2020-01-01");
    • $end = strtotime("2020-02-01");
    • for (let $i=$start; $i<=$end; $i+=86400) { echo date("Y-m-d", $i); }

That should cover the basics, but let us walk through more examples in this guide – Read on!

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

EXAMPLE CODE DOWNLOAD

Source code on GitHub Gist

Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

 

PHP DATE RANGE

All right, let us now walk through the examples on how to create a date range in PHP.

 

1) USING DATE-TIME OBJECTS

1-date-time.php
<?php
// (A) START & END DATE
$start = new DateTime("2020-01-01");
$end = new DateTime("2020-03-02");
 
// (B) DAILY INTERVAL
// USE P2D FOR 2 DAYS, P3D FOR 3 DAYS, AND SO ON...
echo "DAILY<br>";
$interval = new DateInterval("P1D");
$range = new DatePeriod($start, $interval, $end);
foreach ($range as $date) {
  echo $date->format("Y-m-d") . "<br>";
}
 
// (C) WEEKLY INTERVAL - CAPTAIN OBVIOUS P7D
echo "WEEKLY<br>";
$interval = new DateInterval("P7D");
$range = new DatePeriod($start, $interval, $end);
foreach ($range as $date) {
  echo $date->format("Y-m-d") . "<br>";
}
 
// (D) MONTHLY INTERVAL
echo "MONTHLY<br>";
$interval = new DateInterval("P1M");
$range = new DatePeriod($start, $interval, $end);
foreach ($range as $date) {
  echo $date->format("Y-m-d") . "<br>";
}

Well, this should be pretty self-explanatory.

  • First, define the start and end dates –
    • $start = new DateTime("YYYY-MM-DD")
    • $end = new DateTime("YYYY-MM-DD")
  • Specify the interval – $interval = new DateInterval("INTERVAL").
    • PXD Intervals of X days.
    • PXM Intervals of X months.
    • PXY Intervals of  X years.
  • Then, create the range itself – $range = new DatePeriod($start, $interval, $end).
  • Lastly, loop through the range to get the dates – foreach ($range as $date) { $date->format("Y-m-d"); }

P.S. The intervals can go down to even hours, minutes, and seconds. Will leave a link in the extras section below to the PHP manual.

 

 

2) USING UNIX TIMESTAMPS

2-unix.php
<?php
// (A) START & END UNIX TIMESTAMPS
$start = strtotime("2020-01-01");
$end = strtotime("2020-03-02");
 
// (B) DAILY INTERVAL (1 DAY = 86400 SECONDS)
echo "DAILY<br>";
for ($i=$start; $i<=$end; $i+=86400) {
  echo date("Y-m-d", $i) . "<br>";
}
 
// (C) WEEKLY INTERVAL (1 DAY = 604800 SECONDS)
echo "WEEKLY<br>";
for ($i=$start; $i<=$end; $i+=604800) {
  echo date("Y-m-d", $i) . "<br>";
}
 
// (D) MONTHLY INTERVAL
echo "MONTHLY<br>";
$i = $start;
while ($i < $end) {
  $idate = date("Y-m-d", $i);
  echo $idate."<br>";
  $i = strtotime($idate . "+1 month");
}

For those who are new, the Unix Timestamp is simply the number of seconds that have elapsed since 1 Jan 1970.

  • First, get the Unix Timestamps for the start and end dates.
    • $start = strtotime("YYYY-MM-DD")
    • $end = strtotime("YYYY-MM-DD")
  • To create the date range, we simply loop through the timestamps at respective seconds intervals.
    • 1 day has 86400 seconds, so for a daily range – for ($i=$start; $i<=$end; $i+=86400) { ... }
    • 1 week has 604800 seconds, so for a weekly range – for ($i=$start; $i<=$end; $i+=604800) { ... }
    • Take note of the monthly range though, not all months have exactly 30 days. So the safest way to loop is to use strtotime("YYYY-MM-DD +1 MONTH") to calculate the exact “timestamp for the next months”.

 

 

EXTRAS

That’s all for the main tutorial, and here is a small section on some extras and links that may be useful to you.

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *