i want to add two date intervals to calculate the total duration in hours and minutes in fact i want to perform addittion as shown below:
$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 />"; echo "Total interval : " . $interval1 + $interval2;
Any idea how to perform this type of interval addition to get the sum of the two intervals in total hours and minutes format in PHP
Introduction ¶A date interval stores either a fixed amount of time (in years, months, days, hours etc) or a relative time string in the format that DateTimeImmutable's and DateTime's constructors support.
The date_diff() is an in-built PHP function used to get the difference between two dates. It returns a DateInterval object on success or false on failure. The date_diff() function accepts two DateTime objects of the two dates being subtracted.
PHP date_add() Function $date=date_create("2013-03-15"); date_add($date,date_interval_create_from_date_string("40 days")); echo date_format($date,"Y-m-d");
We will be using the built-in function date_diff() to get the time difference in minutes. For this, we will be needed a start date and end date to calculate their time difference in minutes using the date_diff() function. Syntax: date_diff($datetime1, $datetime2);
PHP has no operator overloading* so +
with objects makes PHP trying it to convert them to string first, but DateInterval
does not support that:
interval 1: 03:05 interval 2: 05:00 Total interval : 08:05
Instead you need to create a new DateTime
object, then use the add
function to add the intervals and finally display the difference to the reference point:
$e = new DateTime('00:00'); $f = clone $e; $e->add($interval1); $e->add($interval2); echo "Total interval : ", $f->diff($e)->format("%H:%I"), "\n";
Full Example/(Demo):
$a = new DateTime('14:25'); $b = new DateTime('17:30'); $interval1 = $a->diff($b); echo "interval 1: ", $interval1->format("%H:%I"), "\n"; $c = new DateTime('08:00'); $d = new DateTime('13:00'); $interval2 = $c->diff($d); echo "interval 2: ", $interval2->format("%H:%I"), "\n"; $e = new DateTime('00:00'); $f = clone $e; $e->add($interval1); $e->add($interval2); echo "Total interval : ", $f->diff($e)->format("%H:%I"), "\n";
You might also want to consider looking how DateInterval
stores its' values and then extend from it to do the calculation your own. The following example (Demo) is rough, it does not take into account the inverted thingy, it does not (re)set $days
to false
and I have not checked/tested the ISO specification of the period specifier on creation but I think it is enough to show the idea:
class MyDateInterval extends DateInterval { /** * @return MyDateInterval */ public static function fromDateInterval(DateInterval $from) { return new MyDateInterval($from->format('P%yY%dDT%hH%iM%sS')); } public function add(DateInterval $interval) { foreach (str_split('ymdhis') as $prop) { $this->$prop += $interval->$prop; } } } $a = new DateTime('14:25'); $b = new DateTime('17:30'); $interval1 = $a->diff($b); echo "interval 1: ", $interval1->format("%H:%I"), "\n"; $c = new DateTime('08:00'); $d = new DateTime('13:00'); $interval2 = $c->diff($d); echo "interval 2: ", $interval2->format("%H:%I"), "\n"; $e = MyDateInterval::fromDateInterval($interval1); $e->add($interval2); echo "Total interval: ", $e->format("%H:%I"), "\n";
* If you write a PHP extension, it actually is possible (at least sort-of).
This function allows you to combine any number of DateIntervals
/** * Combine a number of DateIntervals into 1 * @param DateInterval $... * @return DateInterval */ function addDateIntervals() { $reference = new DateTimeImmutable; $endTime = clone $reference; foreach (func_get_args() as $dateInterval) { $endTime = $endTime->add($dateInterval); } return $reference->diff($endTime); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With