Printable VersionPrint
Date Range parsing class in PHP

While developing some web bots, I found a need to parse human-entered date ranges into real start and end dates. Here is the class that I ended up using.

Parsing Date Ranges in PHP

How do you parse "May 5-10, 2010" into a start and end date using PHP? That question led me to develop a PHP class to handle it for me. After searching the web, I found a simple date range class that I could modify. The original class simply accepted start and end dates, a range name, and a list of dates that should be excluded from the created date range.

I could not find a parser for date ranges, but the class created by Brian Rock provided me a good starting framework. I didn't need range names or excluded dates, so I dropped those. To allow foreach looping over the object, I implemented the PHP built-in Iterator interface.

The Public Methods

The constructor for this class is very simple. It simply takes a start date and and end date. These dates may be either timestamps or strings. If they are strings, they are parsed by strtotime().

$range = new DateRange('May 5, 2010', 'May 9, 2010');

Method inRange( $date ) takes a single date (again, either a timestamp or a string) and determines if it is withing the start and end dates of the date range.

Methods getStartDate() and getEndDate() are the getter methods for the $startDate and $endDate protected properties, respectively.

Implementing the Iterator interface requires that the following methods be implemented: rewind(), current(), key(), next(), and valid(). These are all straightforward methods that use the protected property $currentDate.

The Static Methods

The static methods parseString( $rangeStr ) and parseMonthYear( $month, $year ) are the meat of the class. Each method returns a DateRange object if it can successfully parse its parameters into a date range. They return null if they cannot.

$range = DateRange::parseString( 'May 5-9, 2010' );

The parseString() method is currently US-centric in design, but can easily be extended to deal with other date range formats. It can handle ranges within a single month, as seen above, ranges spanning months within a given year, and ranges that span multiple years.

$range = DateRange::parseMonthYear( 'May', 2010 );

The parseMonthYear() method accepts two parameters. The first parameter, $month, can be either a string or an integer from 1 to 12 (January = 1, February = 2, etc). If it is a string, it can be either the full name of the month, or the standard abbreviation. For a list of valid strings, see the cal_info() php function, and the 'months' and 'abbrevmonths' arrays that are returned by it.

DateRange as an Iterator

Since the Iterator methods have been implemented, it is easy to loop over the DateRange object.

$range = new DateRange('May 5, 2010', 'May 9, 2010');
if ( !is_null( $range ) )
    foreach( $range as $day )
        echo date('Y-m-d', $day);

The code above will output


Source Code and Licensing Information

You may view the source code as a text file.

This code is licensed under GNU General Public License, version 3, or any later version, at your option. Article Manager module by by George! Software.