How to get the integer value of "days" from a DateInterval

1

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');

    
asked by anonymous 08.07.2015 / 14:06

2 answers

1

I did it that way and it worked:

$var = $diferenca->days;

$var received the value 36 I wanted!

    
08.07.2015 / 14:32
0

Following the PHP manual you can use format('%a') to format a date difference, or days .

Both give what you seek.

format('%a')

In the manual says

  

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

Example: link

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 .

Example: link

    
08.07.2015 / 14:39