How do I get the days
value of a DateInterval
through the date_interval_format()
method?
I tried the following, but it did not work:
$var = date_interval_format($diferenca, '%days');
How do I get the days
value of a DateInterval
through the date_interval_format()
method?
I tried the following, but it did not work:
$var = date_interval_format($diferenca, '%days');
I did it that way and it worked:
$var = $diferenca->days;
$var
received the value 36 I wanted!
Following the PHP manual you can use format('%a')
to format a date difference, or days
.
Both give what you seek.
format('%a')
Total number of days as a result of DateTime :: diff ()
that is the total number of days between the two dates .
Example:
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-23');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%a'); // dá 12
days
It also says in the Manual of the Datetime Class that have a days
property whose description is: / p>
days
If the DateInterval object was created by DateTime :: diff (), then this is the total number of days between the start and end dates. Otherwise, days will be FALSE.
Seriously translated:
If the interval object is created with DateTime :: diff () then it will be the total number of days between the dates. Otherwise the value will be false
.