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 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 UsageHere are a couple of recognizable, cleanly formatted date strings:
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 The UNIX timestamp will look something like this:
FlexibilityThe beauty is that You can also pass other date/time text references:
All of these are accepted by ApplicationNow 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 Adding to, or subtracing from dates is as easy as a basic math expression:
Here we add one day to "10/10/2008."
This would display:
Wrap-upI hope this brief introduction to PHP's
Comments/Mentions
|
Editor Picks
Email NewsletterSubscribe to the digest newsletter to receive posts by email: Recent Comments
Advertisements
|
Can you put some examples with syntax?