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 $date) { echo $date->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) { $day = date("Y-m-d", $i); }

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

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Date Range Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

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, create two new DateTime() objects for the start and end dates.
  • Specify the required interval range – new DateInterval("INTERVAL") object.
  • Then create the range itself – $range = new DatePeriod(START, INTERVAL, END)
  • Lastly, just loop through the range to get the dates – foreach ($range as $date) { ... }

 

 

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");
}
  • First, get the Unix Timestamps for the start and end dates – strtotime(DATE). For those who do not know, the Unix Timestamp is simply the number of seconds that have elapsed since 1 Jan 1970.
  • 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(DATE NOW + 1 MONTH) to calculate the exact “timestamp for the next months”.

 

 

USEFUL BITS & LINKS

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

 

INFOGRAPHIC CHEAT SHEET

PHP Date Range (click to enlarge)

 

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 *