-1

I have always had a problem with adding and subtracting time like for an example:

   10h:34min 
 + 07h:46min
 -----------
    XX:XX
Anthony Forloney
  • 90,123
  • 14
  • 117
  • 115
  • 3
    possible duplicate of http://stackoverflow.com/questions/2767068/adding-30-minutes-to-time-formatted-as-hi-in-php – Gordon May 05 '10 at 14:37
  • 1
    possible duplicate of http://stackoverflow.com/questions/2382458/subtracting-a-certain-number-of-hours-days-months-or-years-from-date – Gordon May 05 '10 at 14:38
  • 1
    possible duplicate of http://stackoverflow.com/questions/1370332/add-two-or-more-time-strings-in-php – Gordon May 05 '10 at 14:39
  • 2
    possible duplicate of http://stackoverflow.com/questions/1456886/adding-time-in-php – Gordon May 05 '10 at 14:39
  • 8
    Whoa, wait, this site has a search function? – zildjohn01 May 05 '10 at 14:44
  • Also, you might want to specifcy the XX:XX part. What is the addition supposed to return when the hour exceeds 23? Should it keep counting hours or add 1d to the output? What if subtracting would yield a negative number? – Gordon May 05 '10 at 14:52

1 Answers1

1

Convert your time(s) to minutes, add them and re-calculate hours and minutes:

time in minutes ("tim"): (10 * 60 + 34) + (7 * 60 + 46)
result: floor(tim/60) : (tim%60)

floor(tim/60) will give you the whole hours
tim%60 is the "modulo" which is the integer "rest" of (tim/60)

If you work with whole dates (and times, not durations like it seems you do), try mktime and/ or strtotime which support operations like "+10 minutes" (and other).

Select0r
  • 12,234
  • 11
  • 45
  • 68