PHP dateRange Class
dateRange is a fairly simple php class. It’s main function is to create a range of dates – allowing you to quickly check to see if a given date is within that range.
You can view the source code in text format. Or read on for a description of how this can be used.
Components of the dateRange Class
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”).
Optional Parameters for Constructor
If you don’t pass a name to the constructor, it will have a default name of “Random Date Range.”
You can pass either a single date or an array of dates (again, timestamps or strings) for the fourth parameter ($exclusions). If you pass no dates, then there are no exclusions – and every date from $start to $end is considered in the range.
Public Methods
The class is designed with its properties private. Therefore you need to use the methods to access their values.
getName()– returns the name of the dateRangegetStart()– returns the start date of the dateRangegetEnd()– returns the end date of the dateRangeaddExclusion($exclusion)– adds the values in $exclusion to the dateRange’s own $exclusion array. This value can either be a single date or an array of dates. It can also be formatted as Unix timestamps or date strings.inRange($date)– this checks to see if a given date is in the range and not excluded. Returns true if it’s in the range, returns false if not.
Private Methods
The class includes one private method at the moment – isExcluded. This cycles through the $exclusions array and checks to see if a given date is excluded. It’s used in the public inRange($date) method.
Possible Uses
Well, heck, isn’t that your job?
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.
Sample Code Showing How to Use It
Here’s a little sample php to show you how to implement this…
$range = new dateRange("21 March 2008",
"21 June 2008", "Spring 08");
// This will be true
if ($range->inRange("15 April 2008"))
echo "Tax day is in the range "
. $range->getName();
// This will be untrue
if (!($range->inRange("18 Feburary 2008")))
echo "My b-day is not in the range "
. $range->getName();
// Tax day is evil, so we're going to take it out of Spring
$range->addExclusion("15 April 2008");
if (!($range->inRange("15 April 2008")))
echo "Tax day is not in the range "
. $range->getName();
If you’d like to see this script extended, let me know what you think would be useful. Also leave a comment if you run into any errors or problems. Can’t guarantee that I can help you… but if I have some free time I will.
Steve Adams said this on May 20th, 2010 at 5:55 am
Thanks for posting this. I enhanced this class so that it can parse strings and create date range objects from the extracted start and end dates of the string. My version is available at http://adamsmanor.net/article6.html
Marshall Heinig said this on June 15th, 2010 at 6:06 am
Nice Post! I was searching for free classified advertising and classified related articles when I came across your website post on Bing. This is exactly what I was looking for. Thanks for the share. I’ve saved this post for future reference
Nice article – Catch Ya
Katelin Skafec said this on October 26th, 2010 at 4:15 pm
Skip Hop Studio Diaper Tote Handbag is awesome.So several wallets – and also not these modest, useless pockets, either. All of the actual storage compartments tend to be a fantastic dimension and truly help keep every thing organized. The handles are a good length and fit nicely over the shoulder; the tackle shoulder straps additionally stay put on my own shoulder, which is important as soon as you are transporting a child. The tote looks fantastic, too. Not too fancy, but not as well casual. (I’ve it in black) The material is soft (can’t feel of a much better word) so it is simple to squeeze in to tight spaces – but yet it is sturdy. I have a Fleurville Lexi carry and I adore it, too, but that bag is kind of inflexible. I believe that Skip*Hop will grow to be my everyday bag. It’s a little bit big – so if you’re not really in to huge bags this may be much better as an over-night baby diaper tote.
Franklyn Mcclaren said this on December 2nd, 2010 at 6:10 am
When evaluating article submission sites as to whether or not to distribute content to them, you must pay special attention to the alexa ranking and not so much to the directory’s pagerank value.
Giovanni Adderly said this on January 24th, 2011 at 9:59 am
Amazing blog! Do you have any tips and hints for aspiring writers? I’m hoping to start my own website soon but I’m a little lost on everything. Would you propose starting with a free platform like WordPress or go for a paid option? There are so many choices out there that I’m totally confused .. Any tips? Many thanks!
Luigi Kilson said this on February 9th, 2011 at 11:11 pm
magnificent submit, very informative. I ponder why the opposite specialists of this sector don’t notice this. You must proceed your writing. I am sure, you have a huge readers’ base already! – Elegant London Escorts, 65-67 Brewer Street, Floor: 2, London W1F 9UP. Phone: 020 3011 2941
Connie Mikkola said this on May 18th, 2011 at 7:34 am
I would like to use the opportunity of saying thanks to you for your professional guidance I have continually enjoyed browsing your site. I am looking forward to the particular commencement of my college research and the overall planning would never have been complete without consulting your blog. If I might be of any help to others, I’d be ready to help via what I have discovered from here.
wypoczynek Tunezja said this on July 7th, 2011 at 12:48 am
I have not checked in here for a while since I thought it was getting boring, but the last several posts are good quality so I guess I’ll add you back to my daily bloglist. You deserve it my friend
Pterker said this on August 8th, 2011 at 4:55 pm
Great One…
I must say, its worth it! My link, http://blog.livedoor.jp/daisy11a/,thanks haha…
http://www.montgomeryadvertiser.com/html/fitness.html said this on August 29th, 2011 at 2:55 pm
{I am|I’m} {extremely|really} impressed with your writing skills {and also|as well as} with the layout on your {blog|weblog}. Is this a paid theme or did you {customize|modify} it yourself? {Either way|Anyway} keep up the {nice|excellent} quality wr…
Only a monopolist could study a business and ruin it by giving away products….
Iona Mehalic said this on January 4th, 2012 at 3:21 pm
Your house is valueble for me. Thanks!…