How to Calculate Days, Months and Years in PHP

Friends today I am going to tell you about another interesting function of PHP that will calculate the difference of Days, Months and Years between two dates. You can find days, months and years between two dates. You can use this function where you need to count days for billing or for the expiration of the user etc.

Problem to Calculate Days Months and Years in PHP

Suppose you are working on a billing system and you want to calculate the expiration of a user. Or you calculate the difference between two dates. This situation often becomes worst when we don’t know to calculate the difference between two days.

Problem 2

Another scenario is that, for example, you want to calculate the total bill of the user from his joining date to the current date. Or you want to calculate the total time period between two dates for any reason. You can also calculate your age with this function.

The Solution to Calculate Years, Months and Days between two dates

Now you know about the problem and want the solution? Don’t worry and be patient. It is an easy job like other solutions.

We will use PHP built-in DateTime class that will solve our problem with great ease. As you know that now PHP fully supports the OOP structure. So we will use the DateTime class. We also use date() function.

Below is code.

$start_date             =       '2017-12-01';
 $end_date               =       '2017-12-11';       
 $get_start_date         =       new DateTime($start_date); // New date object
 $get_end_date           =       date('Y-m-d',strtotime($end_date));
 $end_date               =       new DateTime($get_end_date); // New date object
 $diff                   =       $end_date->diff($get_start_date);
 $result                   =       $days->format('%d');
 echo $result;

Explanation of Code

As you saw in the code there are only two values that we need. One is start date and the second is the end date. We created an object from DateTime class and then with date() and strtotime() function we used diff() function of DateTime class. In the end we just used format() method to get days between two dates.

Now it is easy to get Years and Months you just change the last line and put %m for months and %y for years in place of %d. Please rate this article if you like it.

Below is the complete detail of format available on PHP officials website for DateTime class:-

Copied from PHP official site.

// Date Should In YYYY-MM-DD Format
// ‘%y Year %m Month %d Day %h Hours %i Minute %s Seconds’        =>  1 Year 3 Month 14 Day 11 Hours 49 Minute 36 Seconds
// ‘%y Year %m Month %d Day’                                     =>  1 Year 3 Month 14 Days
// ‘%m Month %d Day’                                                     =>  3 Month 14 Day
// ‘%d Day %h Hours’                                                        =>  14 Day 11 Hours
// ‘%d Day’                                                                           =>  14 Days
// ‘%h Hours %i Minute %s Seconds’                             =>  11 Hours 49 Minute 36 Seconds
// ‘%i Minute %s Seconds’                                                =>  49 Minute 36 Seconds
// ‘%h Hours                                                                        =>  11 Hours
// ‘%a Days                                                                           =>  468 Days

End of referenced data.

Now we will write a function to make it useful in future. Here is function:

function count_date($start_date,$end_date,$type='d'){
     $start_date             =       new DateTime($start_date);
     $end_date               =       date('Y-m-d',strtotime($end_date));
     $end_date               =       new DateTime($end_date);
     $get_val                =       $end_date->diff($start_date);
     $result                 =       $get_val->format('%'.$type);
     return $result;

I think nothing is complicated in this function. Everything has been explained earlier. Now your function is ready to use. Just pass start date, end date, and type or format name and echo it like below:

echo count_date('2017-12-01','2017-12-11','m');

I hope you understand. However, If you need help with this article then leave a comment or send us a message HERE. You may visit our Web Based Application related to POS HERE.

Leave a Reply - I will show after approval