Date range with PHP DateTime, DateInterval and DatePeriod
DemoToday we will have a look at a way of generating a date range array containing a year and its months. This kind of array can be used to generate a dropdown menu for monthly reports etc.
During this tutorial we will make use of the PHP's DateTime, DateInterval and DatePeriod classes, and to make it work you will need PHP version 5.3 or higher.
First we need to create a start date and format it accordingly:
$start_date = DateTime::createFromFormat( 'Y-m-d H:i:s', '2010-12-01 00:00:00' );
Now with our start date ready, let's create our end date, indicate the interval we want to use and finally create a date range using the DatePeriod class:
$end_date = new DateTime();
$interval = new DateInterval("P1M");
$dates = new DatePeriod($start, $interval, $end_date);
In the above example we've created a new DateTime object, which, when used without any parameters, represents the current date and time.
Next we've created the instance of the DateInterval class and passed the P1M to is as a parameter, which represents 1 month. For more details on interval specification please visit php.net.
Last item in the above block of code simply creates a new date range using the DatePeriod class with 3 parameters: start date, interval and end date.
We are now ready to loop through the results and add them to our array:
$out = array();
if (!empty($dates)) {
foreach($dates as $dt) {
$out[] = array(
'month' => $dt->format('m'),
'year' => $dt->format('Y'),
'month_year' => $dt->format('F Y')
);
}
}
Because we are looping through the DatePeriod object results we can use the reference to its format method in order to return the value in the required format.
If we now print our array:
<pre> <?php echo print_r($out); ?> </pre>
we will get the result similar to this one:
Array
(
[0] => Array
(
[month] => 12
[year] => 2010
[month_year] => December 2010
)
[1] => Array
(
[month] => 01
[year] => 2011
[month_year] => January 2011
)
[2] => Array
(
[month] => 02
[year] => 2011
[month_year] => February 2011
)
[3] => Array
(
[month] => 03
[year] => 2011
[month_year] => March 2011
)
[4] => Array
(
[month] => 04
[year] => 2011
[month_year] => April 2011
)
[5] => Array
(
[month] => 05
[year] => 2011
[month_year] => May 2011
)
[6] => Array
(
[month] => 06
[year] => 2011
[month_year] => June 2011
)
[7] => Array
(
[month] => 07
[year] => 2011
[month_year] => July 2011
)
[8] => Array
(
[month] => 08
[year] => 2011
[month_year] => August 2011
)
[9] => Array
(
[month] => 09
[year] => 2011
[month_year] => September 2011
)
[10] => Array
(
[month] => 10
[year] => 2011
[month_year] => October 2011
)
)
What we could do now with the result is to create a dropdown menu like so:
<select name="year-month">
<option value="">Select one</option>
<?php if (!empty($out)) { ?>
<?php foreach($out as $row) { ?>
<option value="<?php echo $row['year'].'-'.$row['month']; ?>">
<?php echo $row['month_year']; ?>
</option>
<?php } ?>
<?php } ?>
</select>
If you'd like to reverse it so that the latest date is at the top then simply use the array_reverse function before you start looping through the output array:
$out = array_reverse($out);




Simon on Wednesday, 4th April 2012
Thanks, this helped a lot!
Reply