Php: datetime arithmetic

I am a little stuck with the DateIntervalPHP class . What I really want is the number of seconds elapsed between the two marks DateTime.

$t1 = new DateTime( "20100101T1200" );
$t2 = new DateTime( "20100101T1201" );
// number of seconds between t1 and t2 should be 60

echo "difference in seconds: ".$t1->diff($t2)->format("%s");

But all I get is zero. Class is DateIntervalnot suitable for arithmetic? How can I get the “exact” number of seconds (or hours or something else) between two timestamps?

+3
source share
1 answer

If you just want seconds fast, you can use

$diff = abs($t1->getTimestamp() - $t2->getTimestamp());

0, 0, 1 (1 , 0 ). % i, 1, $t1 $t2.

+9

Source: https://habr.com/ru/post/1750895/


All Articles