How to Get Difference Between Two Dates in PHP?

Do you want to calculate difference between two dates in PHP? I mean difference between two dates in days, months, years using php. if yes than i will show you to PHP get difference between two dates using strtotime(), abs() and floor().

We may sometime require to get difference between in PHP for your application, even if you use any php framework like laravel, codeigniter, wordpress but you can use code PHP code anywhere. So you can see following examples.

Bellow examples will help you to calculate difference between two dates in php example.

<?php

$startDate = “2018-05-20”;

$endDate = “2019-08-27”;

$diffData = abs(strtotime($endDate) strtotime($startDate));

$yearsDiff = floor($diffData / (365*60*60*24));

print_r(“Years:”.$yearsDiff);

$monthsDiff = floor(($diffData $yearsDiff * 365*60*60*24) / (30*60*60*24));

print_r(” Months:”.$monthsDiff);

$daysDiff = floor(($diffData $yearsDiff * 365*60*60*24 $monthsDiff*30*60*60*24)/ (60*60*24));

print_r(” Days:”.$daysDiff);

Output:

Years:1 Months:3 Days:9

About Author

Leave a Reply

Your email address will not be published. Required fields are marked *

PAGE TOP
error

Enjoy this blog? Please spread the word :)

RSS
Follow by Email