How to compare dates with PHP

Many people are having trouble comparing dates with PHP, so I decided to introduce one function which makes the process very easy. The function is called strtotime and it is used to convert regular dates(Ex.: 2012-08-26 20:26 ) to Unix/POSIX time. You can check the definition of Unix/POSIX time in WikiPedia.
Now lets see the code:

In order to get the current timestamp you can use:

The power of strtotime, comes from the fact that it recognizes some keywords:

Now, the interesting(and relevant) part…comparing dates.

That is pretty much it…you can now use the function to build date filters and any kind of functionality involving dates.

About Pavel Petrov 2 Articles |  17 How-tos
Pavel is a senior developer for the last 7 years, with extended interest in Linux administration, WordPress and Symfony.

Be the first to comment

Leave a Reply

Your email address will not be published.