datetime - PHP: Wrong Time Calculation


I am working on a project and writing a function to add two different times. The times are stored in database as a string.


  • Pulling value from db
  • converting it into time usingstrtotime
  • adding times usingdate function

Here is my code:

$time_1     = '1:00';
$time_2 = '0:05';

//should be 1:05, whereas it prints 04:05
echo date("H:i", strtotime($time_1) + strtotime($time_2));

Please tell me, what is wrong with above code and how it can be fixed?





Use DateTime::createFromFormat function, and taking ideas from Adding two DateTime objects in php

$time_1 = '1:00';
$time_2 = '0:05';
$t1 = DateTime::createFromFormat('G:i', $time_1);
$t2 = DateTime::createFromFormat('G:i', $time_2);

$interval1 = $t1->diff(new DateTime('00:00:00')) ;
$interval2 = $t2->diff(new DateTime('00:00:00')) ;

$e = new DateTime('00:00');
$f = clone $e;
$total = $f->diff($e)->format("%H:%I:%S");

Additional Details:

  • G and H 24-hour format of an hour with or without leading zeros
  • i Minutes with leading zeros 00 to 59

People are also looking for solutions to the problem: Xdebug is not enabled in PHP Version 7.1.21, XAMPP


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.