PHP date/time comparison and conversion: strtotime()

October 13, 2008

Dealing with dates and times in web programming can be a challenge, especially since there are so many different formats to work with.

For example, storing dates in MySQL has to match YYYY-MM-DD format. However, your user input screens will probably allow for more recognizable formats such as MM/DD/YYYY. Another example is dates in RSS, which have to be in RFC-822 format.

Having to convert those dates to MySQL format when storing data, and then back to the proper format when retrieving data, can often add more complexity to your application.

And I haven't even mentioned having to perform date and time arithmetic and comparisons (ie: is one date/time greater than another, etc).

PHP's strtotime function can come in handy for a lot of the drama associated with date/time conversion and comparison.

When you pass strtotime a "date string," you'll be returned the UNIX timestamp for that date/time. A UNIX timestamp is simply a tally of the number of seconds that have passed since January 1, 1970. With this number, you can now more easily perform date/time comparisons since every date is brought down to it's most basic level - seconds.

Usage

Here are a couple of recognizable, cleanly formatted date strings:

  • 2008-10-10 (MySQL format)
  • 10/10/2008 (preferred display format)
  • Fri, 10 Oct 2008 14:45:08 +0000 (RSS format)

Most of your web applications will use or display these formats quite often.

Imagine having to compare two dates using a different format. You'd first have to somehow convert the dates to the same format, then perform some sort of date/time comparison.

With strtotime, just pass any of those date formatted text examples above, and you'll get a clean UNIX timestamp in return.

The UNIX timestamp will look something like this:

1223622000 (for "2008-10-10")

Flexibility

The beauty is that strtotime isn't strict with the date/time string that you pass to the function.

You can also pass other date/time text references:

  • "now"
  • "today"
  • "next week" (a week from today)
  • "+3 weeks" (three weeks from now)

All of these are accepted by strtotime, and converted to a UNIX timestamp. Being able to supply more "human readable" date text is an added bonus. You never know when your application may need to accept such values from users.

Application

Now that you've converted everything to UNIX timestamps, you can more easily handle and compare dates and times.

If you feel inclined, change your MySQL date/time fields to int(11) and store the UNIX timestamp instead 1. You'd have to remember to always convert on the way out, but it certainly eases backend maintenance.

Adding to, or subtracing from dates is as easy as a basic math expression:

1223622000 + 86400 = 1223708400

Here we add one day to "10/10/2008."

echo date("m/d/Y h:i:s", 1223622000 + 86400);

This would display:

10/11/2008 12:00:00

Wrap-up

I hope this brief introduction to PHP's strtotime function is helpful to those looking to more easily manage dates and times in PHP applications.

  1. Be careful if your PHP and MySQL servers are in different time zones. This has been noted to cause issues.

Comments/Mentions

# Robert at 12/5/2008 7:52 am cst

Can you put some examples with syntax?

# jayess at 2/3/2009 9:31 am cst

Just a suggestion for those of us who don't live in the US. Perhaps having the day and months as different numerals might be helpful.

10/11/2009
11 October 2009

is clearly us format (i.e. wrong for everyone else on the planet).

10/10 for 10th October could either be US format or rest of world format.

Unless you're doing this deliberately to see who can read MySql dates ;-)

# Lau at 3/2/2009 11:21 pm cst

even better than 10/11/2009 is 10/29/2009 because it is abslutely clear which is the day and which is the month.

# Barrett at 7/27/2009 8:08 am cst

strtotime is great...IF your dates are within the 1901-2038 date range convered by unix timestamps. Outside that, you're pretty much, from what I can gather, hosed unless you're using PHP 5.3+ where the the DateTime::diff (http://us2.php.net/manual/en/datetime.diff.php) method becomes available.