Subtract Datetime variables

Asked

Viewed 130 times

1

I have two datetime fields and would like to know the difference between them. I am developing in Php Gtk, so the functions have to be as low as possible.

1 answer

0


One of the ways to do this oriented to objects, is using the class Datetime, it has the diff method that returns a Dateinterval object, which represents the interval between two distinct dates:

Following the example of dates:

  $data1 = new DateTime( '2013-12-11' );
  $data2 = new DateTime( '1994-04-17' );

  $intervalo = $data1->diff( $data2 );

  echo "Intervalo é de {$intervalo->y} anos, {$intervalo->m} meses e {$intervalo->d} dias"; 

Source: How to calculate the difference between two dates?

Browser other questions tagged

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