HighlyStructured.com is a blog website run by Mike D'Agostino about search engine positioning, online marketing, php/MySQL, tae kwon do, and various other topics.

Comparing Dates in PHP

January 18, 2006

When I first started working with PHP to create dynamic website content, it was mostly for simple directory projects. This included a simple admin with the ability to add, edit, and delete pure text. I remember the very first commercial project I worked on involved a directory listing of businesses, organized by location. Each listing had a name, address, city, state, zip code, phone number, fax number, website (if it had one), and main contact.

The directory was location-based, so a viewer could select a state, and then a city to view relevant listings. It started out simple enough, but over time started to grow more complex as more functionality was added (to make the site more compelling to the visitor of course). At one point we decided to add coupons to the site so that visitors could find a business they were looking for, and be able to print out coupons on-the-fly. This was great, but it involved putting an expiration on the coupons.

At this point we had two options: (a) manually "de-activate" the coupons via the administrative interface, which mean we would have to keep a spreadsheet, or refer to something basically every day to determine which coupons had expired, or (b) write a script to automatically de-activate the coupons when they reached their expiration date. Obviously option (b) was the way to go, and so I started out on incorporating dates for the first time into a MySQL database using PHP.

Entering dates was easy enough. I created an expiration date field and chose the MySQL variable type "date". The default value for this, as prescribed by MySQL, is YYYY-MM-DD. I even created three separate drop-down boxes to ensure anyone using the administrative interface didn't input the dates incorrectly. So far so good...

Once this was done and the coupons were set up and everything was integrated into the front-end of the website, it was now time to tackle the final challenge of determining if the coupons were past their expiration date. The pseudo-code was simple enough:

1. Get today's date
2. Compare today's date to the coupon's expiration date
3. If the coupon's expiration minus today's date is positive, the coupon is valid

First I had to find out how to get today's date in PHP. Again simple enough. Using the "date" function in PHP, I "configured" it to represent today's date in the YYYY-MM-DD format:

$todays_date = date("Y-m-d");

Now was the tricky part, comparing the two dates. Having been stuck with two dates in the YYYY-MM-DD format, I really had to do some experimenting. Without going into too much detail, I was left to compare each segment of the date. I had to use the PHP explode function to break apart each segment into an array, and compare values using a long string of if-then statements. If today's year was less than the coupon's expiration date year, it was expired. If it the years were the same, then I had to compare the months. If the months were the same, I had to compare days.

Suffice to say the method actually worked, but took a very long time to put together (although I learned a lot about dates, the explode/implode PHP functions, and array) and was not a great method. I decided there must be an easier way, but would wait until I had to compare dates in a future project to figure it out.

Well the project came up sooner rather than later, and I balked at having to go through the awful if-then statements to compare dates in PHP again. That's when I found about the great strtotime PHP function. Basically PHP will convert just about any type of "time parameter" and convert it into a Unix Timestamp. The Unix timestamp is really great because it's literally a count of seconds. If the moment "right now" starts out at 0 seconds, 2 hours will be 7200 seconds from now. Once you have dates stored as a Unix Timestamp, you simply need to compare which one is larger. (In reality, the Unix Timestamp is the number of seconds since the epoch - January 1 1970 00:00:00 GMT)

Now the whole date comparison process becomes much simpler. Just convert your dates to a Unix Timestamp, compare them, and voila, you have your results! It might look something like this:

$exp_date = "2006-01-16"; $todays_date = date("Y-m-d"); $today = strtotime($todays_date); $expiration_date = strtotime($exp_date); if ($expiration_date > $today) { $valid = "yes"; } else { $valid = "no"; }

And there you have it! To take it a step further, when you know you'll be comparing dates a lot, instead of storing your dates as a "date" in MySQL, store them as strings and convert all dates to a Unix Timestamp before they get stored.

Technorati Tags:           

Recent Articles

Topics

Archive

Other Blogs of Interest