Thursday, 9 August 2018

Calculate The Difference Between Two Dates In PHP

This is a simple guide on how to calculate the difference between two dates in PHP. Be sure to try out the examples below if you’re new to the topic.

Our dates.

Let us say, for example, that we have two date strings and that they are formatted like so:
  1. 2013-03-01 19:12:45
  2. 2014-03-01 06:37:04
As you can see, there’s about a year in the difference between them.
However, what if we want to calculate the exact number of seconds that have passed between date one and date two?
If you run the code above, you’ll see that the answer is: 31490659. i.e. 31490659 seconds passed between “2013-03-01 19:12:45” and “2014-03-01 06:37:04”.
A basic drill down of what I did:
  1. I converted both of our date strings into a unix timestamp by using the built-in PHP function strtotime. A unix timestamp represents the number of seconds that have passed since 00:00 on the 1st of January, 1971. By converting them into Unix Time, I’ve converted the dates into a format that allows me to do some basic calculations.
  2. I then subtracted the older date from the newer date.
Remember: The older a date is, the smaller its corresponding timestamp will be! Run the following example to see what I mean:
As you can see, “2014-06-07” has a much bigger timestamp, simply because more seconds have passed since the 1st of January, 1971 and the 7th of June, 2014.

Between then and now.

What if you need to find out how many seconds have passed since a given date?
In the code above:
  • I’ve used the function time to retrieve the current timestamp. i.e. Our current Unix Time.
  • I then subtracted the older timestamp from our current timestamp.
If you continue to refresh the page, you’ll see that the difference in seconds will continue to grow.

Minutes & Days…

In many cases, you’ll want the number of days or minutes that have passed. Let’s face it: Seconds are kind of useless to an end user.
To calculate the number of days that have passed:
As you can see, I was able to convert seconds into days via some basic math:
  • There are 60 seconds in a minute.
  • There are 60 minutes in an hour.
  • There are 24 hours in a day.
If you multiply the figures above, it will give you 86400, which is total the number of seconds that is in a given day. Divide the difference in seconds between our two dates by 86400 and you’ve got the number of days that have passed.
You can then use the floor function in order to round the result down. i.e. 2.9 days = 2 days.
To get the number of minutes, you can simply divide the difference in seconds by 60, like so:
As you can see, it’s pretty simple.

OOP

Once you’ve grasped the fundamentals of dealing with dates and timestamps in PHP, you should look into using the DateTime class, simply because it’s cleaner and it provides you with an OO interface.
For example, if we want to get the difference in days using the DateTime class, we can do the following:
Much better.
To transform the difference into a human-readable format that is comprised of years, months, days, hours and minutes:
As you can see, the DateTime class is pretty powerful once you figure out to use it!

0 comments:

Post a Comment