DEV Community

loading...

Answer: How to display total number of days between two dates using php

mhsohag11 profile image mhsohag11 ・1 min read

Use the DateTime class and compare them via diff() method. $date1->diff($date2). The output will give you an accurate result of days:

$date1 = new DateTime('2018-01-01');
$date2 = new DateTime('2018-02-15');

print_r($date1->diff($date2));

Output:

DateInterval Object 
( 
[y] => 0 
[m] => 1 
[d] => 14 
[h] => 0 
[i] => 0

Discussion

pic
Editor guide