Get Dates Range Array

Generating a Date Range Array in PHP

This short tutorial shows a practical PHP function that builds an array containing every date between a starting date and an ending date. It is handy whenever you need to loop through a continuous span of days, for example in reports, calendars or scheduling logic.

How the function works

The helper create_dates_range_array() accepts a start date and an end date in Y-m-d format. It converts both into Unix timestamps with mktime(), then walks forward one day at a time, pushing each formatted date into the result array until the end date is reached. If the end date comes before the start date, an empty array is returned.

Example usage

After defining the function, simply call it with your two dates and print the result with print_r() to see the full list of dates in the range.

Tags: dates, PHP

Tweet Share

Join the Discussion

Have a question or a better approach? Leave a reply below. Required fields are marked, and your email address will never be published or shared.

Leave a Reply

Recent Posts

Recent Comments

A reader shares feedback on the premium banner manager plugin.
Another visitor joins the conversation about UBM Premium.
Tobias on UBM Premium
A comment on the premium version of the banner manager.
Feedback on the 1.5 release of the banner manager plugin.
A reader reacts to the 1.5 version announcement.

Most Commenting Visitors

Avatar of JDB
JDB (13 comments)
Avatar of Laurens V.
Laurens V. (10 comments)
Avatar of wifi
wifi (9 comments)
Avatar of Droid
Droid (9 comments)
Avatar of Marcelo
Marcelo (8 comments)

My Plugins

My Pages

Share and save icon
Share this page
Useful Banner Manager banner
Banner manager
Useful Video Player banner
Video player
Daily deals banner
Daily deals
Facebook icon
Facebook
LinkedIn icon
LinkedIn
Twitter icon
Twitter
Buzz icon
Buzz
Top list badge
Top list
No image placeholder
Placeholder
Quick navigation panel info icon
Quick navigation info

Blorner Facebook LinkedIn Twitter Google+

Tags

counter text texts passing time PHP custom fields categories email quick navigation Blorner outbound links dates

Partners

Categories

DOM JavaScript jQuery MySQL News PHP RSS Wordpress

Home Get Dates Range Array