Comparing Dates with PHP

The White Rabbit Checking the TimeWhen working with dates, time and calendars it becomes a pretty common requirement to compare dates-Testing whether a date occurs after or before one date, whether an event occurs between two specific times, etc. The easiest way to work with dates in PHP is with the DateTime object.

Obviously for us (as humans) it is pretty easy to see that my party is not during my shift. On the other hand for the computer to see this is a whole other issue.

A DateTime object can be instantiated as such:

And is described as:

The string parameter can accept any string that can be converted by strtotime (Read the Date and Time Formats section for a full explanation).

So, back to the instantiation. I want to create an DateTime object for Halloween (October, 31st 2010):

This would yield a DateTime object that references 10/31/2010 in the America/Denver timezone (Maybe more on timezones later). This can be verified by using the DateTime object’s format() method:

This gives the following result:

2010-10-31

Now lets look to see if today is before Halloween. Today is 10/27/2010 which is clearly before Halloween. It should first be noted that strings should never be used to compare dates. Because of the method PHP uses when comparing strings in an if statement the following code:

Results in:

( 10 > 10 )
bool(false)

This is because PHP will first attempt to convert the strings to numbers and compare them numerically. And since 10 is not greater than 10 we get false.

To compare the dates correctly, what needs to take place is

  1. The dates need to be in the same format
  2. Converted to the same type of number
  3. Then compared

Our dates are already in a DateTime object and are therefore in the same format.

Next we need a numeric value of the date with which we can make a comparison. The best way to do this is to use the Unix Epoch Timestamp. A Unix Timestamp is the number of seconds that have elapsed since January, 1st 1970. To convert a date to it’s timestamp, we simply use the format() method with the string 'U':

Which yields:

1288504800

Now we can correctly compare the two dates:

Which gives us, as we would expect:

( 1288504800 > 1288240568 )
Halloween has not passed yet.

Now as you would expect comparing for the reverse, namely that a date occurs after a date, is pretty much the same thing. However now I would like to look at checking to see if a date or time is between two different dates and times. Doing so should be easy now.

I want to see if my Halloween party is scheduled during my scheduled work shift. First off we need some facts:

  1. My work schedule is 10:00 am to 7:00 pm
  2. And my Halloween party is at 7:30 pm

It will be cutting it close. ;) Obviously for us (as humans) it is pretty easy to see that my party is not during my shift (and it better not be since I’m hosting the party). On the other hand for the computer to see this is a whole other issue.

We will work this out in a similar fashion to the last example:

  1. The dates need to be in the same format
  2. Converted to the same type of number
  3. Then compared

First lets set up the DateTime objects:

Next we will convert them to Unix timestamps and compare them:

Result:

You don’t work during your party. FTW!

And I could not be happier that I don’t work during my party.

Hope this short tutorial helped someone. =P Questions-Leave them in the comments.

3 Comments

  1. Reply
    Dee Schindele November 26, 2010

    Love your site man keep up the good work

  2. Reply
    php January 25, 2011

    such a long poster

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">