Page 1 of 1

code for compare two dates in php

Posted: Thu Sep 11, 2014 3:59 pm
by Joshi jahanvi
How can we know the number of days between two given dates using PHP?

Re: code for compare two dates in php

Posted: Wed Oct 08, 2014 1:39 pm
by Guest
Below code may help you.

Code: Select all

$datetime1 = new DateTime("2010-06-20");

$datetime2 = new DateTime("2011-06-22");

$difference = $datetime1->diff($datetime2);

echo 'Difference: '.$difference->y.' years, '
                   .$difference->m.' months, '
                   .$difference->d.' days';

print_r($difference);


Result as below:

Code: Select all

Difference: 1 years, 0 months, 2 days
    [days] => 367