Home >Backend Development >PHP Tutorial >How to Sum Date Intervals in PHP?
In PHP, we may encounter situations where we need to add two or more date intervals to calculate the total duration in hours and minutes. To achieve this sum, we can follow these steps:
<code class="php">$a = new DateTime('14:25'); $b = new DateTime('17:30'); $interval1 = $a->diff($b); echo "interval 1: " . $interval1->format("%H:%I"); echo "<br />"; $c = new DateTime('08:00'); $d = new DateTime('13:00'); $interval2 = $c->diff($d); echo "interval 2: " . $interval2->format("%H:%I"); echo "<br />";</code>
<code class="php">$e = new DateTime('00:00');</code>
<code class="php">$f = clone $e;</code>
<code class="php">$e->add($interval1);</code>
<code class="php">$e->add($interval2);</code>
<code class="php">echo "Total interval: " . $f->diff($e)->format("%H:%I");</code>
The above is the detailed content of How to Sum Date Intervals in PHP?. For more information, please follow other related articles on the PHP Chinese website!