::Community of ACEM students::
HomeHome  CalendarCalendar  GalleryGallery  FAQFAQ  SearchSearch  MemberlistMemberlist  UsergroupsUsergroups  RegisterRegister  Log in  

Display results as :
Rechercher Advanced Search
Latest topics
» Primer Imperio-Torre De Control Mp3
Fri Feb 14, 2014 1:20 pm by bakakali

» LTSP Deployment
Sat May 09, 2009 11:31 am by Santos

» GTA SanAndreas 64kB!!!
Tue May 05, 2009 2:56 pm by Santos

» EPL names..
Thu Apr 30, 2009 4:26 pm by Kxhitiz

» Google logo.
Thu Apr 30, 2009 4:22 pm by Kxhitiz

» Google logo.
Thu Apr 30, 2009 11:36 am by Santos

» the longest website in the world haha
Wed Apr 29, 2009 8:50 am by Kxhitiz

» Ubuntu 9.04 Jaunty Jackalope.
Sat Apr 25, 2009 12:31 am by Santos

» Program to display text with defined cursor position and defined color
Sat Apr 25, 2009 12:11 am by Santos

March 2019

Share | 

 Comparing Dates in PHP

Go down 

Posts : 24
Join date : 2009-03-06

PostSubject: Comparing Dates in PHP   Mon Mar 30, 2009 1:14 pm

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.
Back to top Go down
View user profile


Posts : 7
Join date : 2009-03-07
Location :

PostSubject: Re: Comparing Dates in PHP   Tue Mar 31, 2009 12:50 am

Kxhitiz wrote:
When I first started working with PHP to create dynamic website content, it was mostly for simple directory projects.
Ani Kxhitiz Bro , Afaile nai start garda hora ???????????
Malai ta Mike D'Agostino jasto po laagchha ta !!!!!!!!!
Back to top Go down
View user profile
Comparing Dates in PHP
Back to top 
Page 1 of 1
 Similar topics
» release dates. how do they work (for the wii)
» Lucky / Unlucky days
» Parramatta Toy fair
» Animated GIF in forum avatar
» Mystery gift warnings?

Permissions in this forum:You cannot reply to topics in this forum
ACEM PORTAL :: Programming :: PHP & MySQL-
Jump to: