In my PHP application I want to calculate the sum of two time variables. I am looking for something like this example.
$time1 = 15:20:00;
$time2 = 00:30:00;
$time = $time1+$time2;
The DateTime::add() function is an inbuilt function in PHP which is used to add an amount of time (days, months, years, hours, minutes and seconds) to the given DateTime object.
$min = $interval ->days * 24 * 60; $min += $interval ->h * 60; $min += $interval ->i; // Printing the Result in Minutes format.
MySQL function NOW() returns the current timestamp.
If the answer you expect is 15:50:00
and you want to use strtotime
and date
functions, you need to subtract the seconds $time1
and $time2
share when you transform them to unix timestamps:
$time1 = '15:20:00';
$time2 = '00:30:00';
$time = strtotime($time1) + strtotime($time2) - strtotime('00:00:00');
$time = date('H:i:s', $time);
The best way to do this is most likely to use strtotime to convert them to timestamps and then do the adding together:
$o = strtotime($time1)+strtotime($time2);
If I remember right strtotime
does support this format.
Otherwise you will need to filter it out yourself.
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