PHP dateRange Class 1.0
This class was created to hold a range of dates and make it easy to check if a given date is within that range. You can also set a series of dates to be excluded from this range.For example, create a range of "December 21, 2007" to "March 21, 2008" and call it "Spring." You could then check to see if a given date is within the date range that you called "Spring."
The dateRange class has four main properties:
- $start – the start date
- $end – the end date
- $name – a display name for your daterange
- $exceptions – a list of dates that are considered “out of the range”
Here’s an example of how to create an instance of the dateRange class…
$range = new dateRange("1 January 2008", "31 December 2008", "Year of 2008");
Formatting Dates for Use with dateRange:
The class stores the start and end dates as Unix timestamps, but it is designed to take either a timestamp or a formatted date string as input. In this case, I passed a string to the constructor and it automatically converted it to a timestamp. It uses the php strtotime() function to convert from a string to a timestamp, so you’re free to use any strings that you know that function can normally convert. The simplest format is the one I used – “Day Month Year”).
Here are a few things you might use this for:
- Form validation. If your user is supposed to input a date, check to see if it’s in the range you want it to be.
- Convenience function for a calendar script. You could create a number of dateRanges in your calendar and use this to see if events fall within a certain range.
- Detect the current season. Create a dateRange for each season, and then compare the current time() to see what the current season is.
More popular Date & Time
- Ultimate PHP Date Time Script 1.0.0 (3 years, 6 months 4 days ago)
- Display human friendly relative dates and times 0.9 (3 years, 12 days ago)
- PHP dateRange Class 1.0 (3 years, 6 months 4 days ago)
- CountDown PHP Script (3 years, 2 months ago)
- Opening hours 2.0 (3 years, 6 months 4 days ago)