-2

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

I'm:

  • Pulling value from db
  • converting it into time using strtotime
  • adding times using date 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?

Thanks

Alena
  • 1,134
  • 6
  • 19
  • 45
  • that's kind of difficult, because a lot of calculation depends on those fields. So, I can't change field type. :( Any other way to solve this issue? – Alena Sep 22 '18 at 07:22
  • I can see you want to add two times, but what are you really trying to achive? It is not possible to work with real date/times? Like: '2017-02-08 12:30:21'? Why did you choose to work with '1:00' and '0:05'? Alternatively, you could work with seconds, which would be '3600' and '300', which are easy to add, and convert back to '1:05'. – KIKO Software Sep 22 '18 at 07:24
  • @KIKOSoftware can't work with complete date and time object. The user adds time in hours and minutes for each day, which is stored in db. So, I need to perform calculation on that.... – Alena Sep 22 '18 at 07:28
  • But you could work in seconds, which you neglected to comment on. Believe me, I speak from experience, you want easy to use data in your database. That would also allow you, for instance, to use a MySQL query to perform calculations with your time field. – KIKO Software Sep 22 '18 at 07:37
  • One suggestion here: There are two "times" involved here, a point in time and a timespan. You can only add a timespan (to another timespan or to a point in time), but you can not add two points in time. For example, you can not add last week to yesterday, it just doesn't make sense. Make sure you define which kind of time you mean and then pick the right tools to work with them. – Ulrich Eckhardt Sep 22 '18 at 08:10

2 Answers2

1

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;
$e->add($interval1);
$e->add($interval2);
$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
Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57
1

Your problem is because strtotime returns the number of seconds since the Unix Epoch (Jan 1 1970). So what you are getting is not values of 60 and 5, but something more like 1537570800 and 1537567500. When you add those two values together, you end up with a date far in the future, with what looks effectively like a random time. To compensate for this, you need to subtract the value of strtotime at the start of the day to make the second time a relative time e.g.:

echo date("H:i", strtotime($time_1) + strtotime($time_2) - strtotime('00:00'));

Output:

01:05

Update

Since it turns out that the sum of the two times can exceed 24 hours, the above code will not work (the maximum time it will display is 23:59 before rolling over to 00:00. So it is necessary to convert both times to a relative number of minutes to the start of the day, add them and then display as hours and minutes:

$time_1 = '12:00';
$time_2 = '14:30';
$time_sum = (strtotime($time_1) + strtotime($time_2) - 2 * strtotime('00:00')) / 60;
printf('%02d:%02d', intdiv($time_sum, 60), $time_sum % 60);

Output:

26:30
Nick
  • 138,499
  • 22
  • 57
  • 95
  • 1
    Yes, this will work, of course, but it does encourage Alena to keep the impratical time format in the database. Remember, adding '14:30' and '12:00' would now result in '2:30'. Is that the intention? Or should it be '26:30'? – KIKO Software Sep 22 '18 at 07:35
  • @Nick short and sweet answer! :) – Alena Sep 22 '18 at 08:09
  • @Alena thanks - but a good point has been raised: is it possible that time1+time2 will exceed 24 hours? If so this solution as is won't work, so please let me know so I can update if necessary. – Nick Sep 22 '18 at 08:33
  • @Alena I've added an alternate way of computing the sum that will still work if the sum exceeds 24 hours. – Nick Oct 06 '18 at 00:49