DEV Community

mhsohag11
mhsohag11

Posted on

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

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

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay