PHP date/time comparison and conversion: strtotime()
Posted by Matt Thommes on 10/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.
- Be careful if your PHP and MySQL servers are in different time zones. This has been noted to cause issues.
About the author(s)
Matt is an independent publishing enthusiast, mobile blogger, content creator, informative writer, web developer from a suburb of Chicago. Never one to conform, Matt intends to promote the effect the web has on our lives, in an effort to intensify, instruct, and clarify all that is happening around us.
Comments 
Note: Comments may be viewed by authors, but if you have a more specific question you'd like to ask them, please email matt.thommes@paininthetech.com.
Comment preview:
- Previous: Amazon S3 cascade pricing
- Next: Backing up SMS: how do you do it?
Recent Referers to PHP date/time comparison and conversion: strtotime()
- http://google.com/se ...
- http://dogpile.com/d ...
- http://google.com/se ...
- http://google.cz/sea ...
- http://google.fi/sea ...
# Robert at 12/5/2008 7:52 am cst
Can you put some examples with syntax?