T
T
tatarrr952019-11-03 19:04:57
PHP
tatarrr95, 2019-11-03 19:04:57

Why does date_diff return the wrong number?

I consider the difference between two dates, I can not understand why it gives out such a number.

$format = 'd.m.Y';
$val1 = "05.11.2019";
$val2 = "02.02.2020";
$val1 = date_create_from_format($format, $val1);
$val2 = date_create_from_format($format, $val2);
$countOfDays = date_diff($val2 , $val1) -> format("%d");

Why does this code result in $countOfDays = 27? Where does this number come from? I thought it should be around 90.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2019-11-03
@tatarrr95

everything is correct, %d is the number of days within a month if
you want to see the total number of days - use %a

D
Denis, 2019-11-03
@sidni

$countOfDays = date_diff($val2 , $val1)->format("%R%a");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question