Why does DateTime :: diff return 0 days between these dates?

Consider the following test case:

$start = new DateTime("2011-02-25");
$end = new DateTime("2011-03-25");

$interval = $end->diff($start);


echo "INTERVAL = ".$interval->format("%d");  // Should give me the 
                                             // interval in days, right?
echo "START = ".$start->format("Y-m-d");
echo "END = ".$end->format("Y-m-d");

Result:

INTERVAL = 0  <---------- WTF????
START = 2011-02-25
END = 2011-03-25

diff()seems to be claiming that the period between February 25, 2011 and March 25, 2011 is 0 days!

I have to skip something. But what?

+3
source share
1 answer

you need to use the format code 'a'. d- the number of days in the difference, not the number of days in the network. in the dateinterval object days, the corresponding field. in your example you will seem = 1

+7
source

Source: https://habr.com/ru/post/1791630/


All Articles