How to take the entire "days" value of a Dateinterval

Asked

Viewed 117 times

1

How do I get the value days of a DateInterval through the method date_interval_format()? I tried it this way, but it didn’t work:

$var = date_interval_format($diferenca, '%days');

  • Have you tried using something like $difca->format('%d days'); ?

  • Already yes. This way is returned "6 days", and not the 36 I need ! (6 days + 30 days of 1 month)

  • What do you have in $diferenca? seconds? wouldn’t that divide by 606024 and have days? with an optional rounding.

  • The $difference is a variable of the type Dateinterval, as shown in the image above ! And I want to get the value 36 of days

  • You can show where you get that $diferenca?

  • Thus Sergio : $difca = $dataFinal->diff($stardate); I make the difference between two Datetime.

Show 1 more comment

2 answers

1

I did it that way and it worked:

$var = $diferenca->days;

$var received the 36 value I wanted !

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 it says

Total number of days as a result of a Datetime::diff()

namely 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: https://ideone.com/fork/XfSnAI

days

It also says in the Manual of the Class Datetime who own a property days whose description is:

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.

Translated would be:

If the range object is created with Datetime::diff() then it will be the total number of days between dates. Otherwise the value will be false.

Example: https://ideone.com/E8M2n1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.