Convert the Difference of two dates into an integer

1

I want to convert the difference between two dates into an integer, this is the code that makes the difference but it generates a string and I require an integer to use that value in arithmetic operations thanks

<?php
 $datetime1 = date_create('2017-05-26');
 $datetime2 = date_create('2018-05-22');
 $interval = date_diff($datetime1, $datetime2);
 echo $interval->format('%r%a');
?>
    
asked by Alfonso Hortua 22.05.2018 в 16:36
source

3 answers

1

Regarding your doubt, according to the documentation in PHP you could do a "cast" or manipulation of types , try this:

<?php  $diasInt = (int) $interval->format("%r%a");
       var_dump ($diasInt); //para verificar el tipo de la variable
       echo $diasInt;        
?>

I leave the link to the official documentation, contains several examples: link .

Annex image that the code is functional and meets your question.

Greetings.

    
answered by 22.05.2018 в 21:02
0

What you are looking for is a php function that parsea string a int , I am happy to communicate that something like this exists and is such that:

PHP

 intval( mixed $diferenciaFechas [, int $base = 10 ])

What is in brackets is optional, it is only to define on what number base is the number you enter

(eg if you pass a binary number base = 2 , if it was hexadecimal , 16), by default it comes in base 10, which is the usual number system.

I hope it helps you :), if so, it always helps a +1: D

    
answered by 22.05.2018 в 17:05
0

Alfonso, that whole already exists in your object . You just have to get the value inside the object if that's what you're interested in. So you save yourself having to apply output formats and then reconvert again.

If you make a var_dump($interval) you can see that in effect your object has a property days , which is of type int , that is, the data and type you need:

object(DateInterval)#3 (15) {
  ["y"]=>
  int(0)
  ["m"]=>
  int(11)
  ["d"]=>
  int(26)
  ["h"]=>
  int(0)
  ["i"]=>
  int(0)
  ["s"]=>
  int(0)
  ["weekday"]=>
  int(0)
  ["weekday_behavior"]=>
  int(0)
  ["first_last_day_of"]=>
  int(0)
  ["invert"]=>
  int(0)
  ["days"]=>
  int(361)
  ["special_type"]=>
  int(0)
  ["special_amount"]=>
  int(0)
  ["have_weekday_relative"]=>
  int(0)
  ["have_special_relative"]=>
  int(0)
}

That means that you can get the value of that property as it is through your object, which for that you have:

$intDiff=$interval->days;

Like this, you can get any of the other properties you need, without having to resort to other types of manipulations. That is one of the great advantages of working with objects.

Test:

If we test what type is the variable that we just created, taking it from the object directly:

var_dump($intDiff);

Exit:

We will see that it is a variable of type int . That is, just what you are looking for.

int(361)

Doing this saves you two steps:

  • Having to apply format: $strDiff= $interval->format('%r%a');
  • To then have to convert it to whole.
  • answered by 22.05.2018 в 18:01