How to calculate the days between two dates in php
Updated: 14-Apr-2022 / Tags: PHP Tutorials / Views: 746 - Author: George
Introduction
Let's say you are writing a php application, and in some point you want to calculate how many days are between two dates.
Or you have some kind of accounts that have time limits and you want to check if they are still active. If you don't know how to work with dates in php, you are in the right place.
I have prepared an example, in which we are going to find out, if a 30 days subscription is still active or has expired.
This is a follow the Chef tutorial😉, that means that if you like, you can follow along.
So with that said let's start.
The php code
Open your code editor and create a file and name it script.php. Of course you have to be in your localhost folder in order to execute php code.
In the first line we are going to write the opening php tag.
<?php
Next in line 2 we are going to create a variable called $account_created and set its value to a random date, lets say "06-mar-2022". This is going to be our starting date.
<?php
$account_created = "06-mar-2022";
In line 3 we will set the expiration date. We are gonna set that the account will be active for 30 days.
$account_expires = "06-mar-2022 + 30 days";
Let me explain this string "06-mar-2022 + 30 days".
We are taking the starting date "06-mar-2022" and we use
the plus + operator to add 30 days.
This is fully legit when we are dealing with the DateTime object,
as you will see in the next lines.
Next we are going to use the date_create() function to create DateTime objects from our two dates.
The starting and the expiring date.
The date_create function takes as an argument a valid date formatted string and creates a DateTime object from it.
Here are some valid date formats that we can use.
"4/12" month/day,
"12/22/22" month/day/year , "2020/04/12" year/month/day,
"12-04-2022" day-month-year.
you can see all valid formats. Here
// creating objects from the two dates.
$origin = date_create($account_created);
$expire = date_create($account_expires);
In line 6 we are using the date_create function and and we pass-in as an argument the $account_created variable. And we store the returned DateTime object in the $origin variable.
We will do the same thing with the $account_expires variable in line 7.
Now the $origin and $expire
variables are holding each a DateTime object.
PHP has a function that compares two DateTime objects and returns in the form of an object the differences. That function is the date_diff() function. Lets check it out.
$explore = date_diff($origin, $expire);
In line 8 we use the date_diff() function and we pass-in as arguments the $origin, and the $expire DateTime objects. And we store the returned object which holds the differences in the variable $explore.
But lets see how the returned data looks like. Lets do a var_dumb on the $explore variable.
var_dumb($explore);
DateInterval::__set_state(array(
'y' => 0,
'm' => 0,
'd' => 30,
'h' => 0,
'i' => 0,
's' => 0,
'f' => 0.0,
'weekday' => 0,
'weekday_behavior' => 0,
'first_last_day_of' => 0,
'invert' => 0,
'days' => 30,
'special_type' => 0,
'special_amount' => 0,
'have_weekday_relative' => 0,
'have_special_relative' => 0,
));
As you can see the $explore variable is holding an object with a bunch of properties. If you look at the fifth line from the bottom and up, you see the "days" property, and it has a value of 30.
The property "days" tells us how many days are our two dates apart. And as we can see this is the only difference that the two objects have.
We are going to use the date_diff() function later on, to calculate how many days ago has the account expired, or how many days is the account still active.
We can use DateTime objects in an if statement, and we can compare them.
echo ($origin < $expire) ? "True" : "False"; // will give True
The above condition will return true, because the $origin object has less days than the $expire object. And it makes sense, because the $expire object has 30 days more.
As we know from the introduction, in this example we are going to check if a 30 days account has expired, or is still active. Therefore we need one more element, and that is the todays date.
So lets create a DateTime object from todays date, and store it in a variable named $today .
We can use again the date_create() function...
$today = date_create("13-apr-2022");
...but the right way to do it, is to call the DateTime constructor.
$today = new DateTime(); // 13-apr-2022
Now that we have all the information we need we are going to use an if statement and we will compare the $expired object with the $todays object.
if ($expire < $today){
$days_expired = date_diff($expire, $today);
echo "Your account has expired {$days_expired->days} days ago";
}else{
$days_left = date_diff($expire, $today);
echo "Your account will be active for another {$days_left->days} days ";
}
We are checking in line 13 if the $expire object is less than the $today object.
If this is true the 30 days account has expired and we use the date_diff() function to find out how many days ago. To get the days we are targeting the "days" property $days_expired->days.
And in line 16 we have the else clause. When this is true, the $expire object is greater than the $today object. That means that the account is still active and we echo out the number of days that the account will be active.
Summary
We saw how to get the number of days between two dates by using the DateTime object in php.
Last Words
Thanks for reading, i hope you find the article helpful.
Please leave a comment if you find any error's, so i can update the page with the correct code.
Or you just can say hi.
Source code
You can download the source code and use it in any way you like.
Times downloaded: 93