Working with dates in PHP is one of those things that you either have a good handle on, you’re working on understanding, or you’re stuck in the rabbit hole of the documentation that’s in the manual.

Difference in Dates Using PHP: The PHP Manual

If you’re working with WordPress, though, the chance that you’re going to need to work with dates is quite high. Nearly everything that we publish has at least one date associated with it.

This includes post, pages, custom post types, revisions, drafts, and so on.

Furthermore, there’s a chance that custom work that you need to develop with require that you find the difference in two dates using PHP. And though there are multiple ways of doing this, there’s a process that I’ve been following for something.

The Difference in Dates Using PHP

Before I walk through my example, it will help to give a bit of context. After all, whatever you’re working on is likely going to be a bit different than what I’m doing.

Difference in Dates Using PHP: A Function For Doing So

The general idea might still be the same so the gist of what you’re doing can still be extrapolated from the code below. But let me give a concrete example from my own work:

  1. I have a custom post type representing an event and the event as a post date.
  2. I need to know when the event starts. To do this, I define the date format using the n/j/Y format as supported by PHP and then convert this to a date using PHP’s date function.
  3. Next, I take today’s date (as an easy way to determine the difference in dates using PHP for this example) and convert it into a date using the same function and n/j/Y format.
  4. After that, I instantiate two DateTime objects and compute the difference.

If that’s not straightforward, I do recommend clicking on the above links. Otherwise, here’s the code.

Notice in the code above I’m only interested in retrieving the difference in years. You may opt to choose something different. If that’s the case, the API offered by PHP makes it easy to do that by examining the properties of the DateTime object.

But given two dates, this is a straightforward way to find the difference in dates using PHP in the context of WordPress.


Join the conversation! 4 Comments

  1. Nice article with examples Tom! Even though I knew about the ‘difference’ method, I just came to an idea for a better solution with WP Cron events.

    I don’t know if you ever encountered such situation, probably you did. When scheduling WordPress CRON jobs I realized that the timestamp which we are saving will be compared to the server time. If the WordPress settings are set to a different time, the CRON could occur on a different time even though, we specified the time having our WordPress settings in mind.

    With this method, the difference could be easily calculated and applied to the timestamp accordingly (add or subtract). Or maybe I have this all thing a bit wrong?

    • With this method, the difference could be easily calculated and applied to the timestamp accordingly (add or subtract). Or maybe I have this all thing a bit wrong?

      Paying attention to server time is very important as you’ve seen – everything, in terms of date and time when run on the server, is calculated based on the server’s time.

      So I recommend getting the server’s timezone and set it with your instance of DateTime and then proceeding with the rest.

      For some projects, it’s not; for others, it is.

  2. Just a question that i’ve run into more than once: Does this account for timezone offsets in the difference? Probably not extremely relevant in this case since it’s only referring to the year, but it’d be really cool to see an in depth post on post dates and php in reference to timezones and server settings (Can tend to get confusing pretty quick!) since there is a scarce amount of WordPress documentation about it.

Leave a Reply