1
 $time1      = "24:00" //in hours:minutes
 $time2      = "18:32"// in hours:minutes
             ---------
 $difference = "5:28"
             ----------

I need difference look like that.(difference = 5:28) what can I do in php. The values are time durations, not dates and the other post does not answer my question.

I was stuck with that simple calculation.

Bala Murugan
  • 479
  • 5
  • 10
  • already answered but the answer contains date and time. i need time difference – Bala Murugan Oct 07 '15 at 19:51
  • Is the the difference between two *timestamps* [eg `2015-01-01 24:00` and `2015-01-01 18:32`] or the difference between two *durations* [eg: `24 hours, 0 minutes` and `18 hours, 32 minutes`]? – Sammitch Oct 07 '15 at 19:51
  • I dont need timestamp. only need difference of that 2 values. – Bala Murugan Oct 07 '15 at 19:53
  • 1
    from the dupe `$d1=new DateTime("24:00"); $d2=new DateTime("18:32"); $diff=$d2->diff($d1); print_r( $diff ) ;` –  Oct 07 '15 at 19:57
  • Use `explode(':', $time1)`, `$duration1 = $hours1 * 60 + $minutes1`, calculate the difference, do math to convert it to hours and minutes, display. – Sammitch Oct 07 '15 at 19:58
  • @Sammitch, dont, use the proper date\times tools –  Oct 07 '15 at 19:58
  • 1
    @Dagon you can't use DateTime to diff two durations. You will get errors due to timezones, leap days/seconds, and DST. – Sammitch Oct 07 '15 at 19:59
  • you can: http://codepad.viper-7.com/7XmsB4 –  Oct 07 '15 at 19:59
  • @Dagon you can, except for when any of the things I said happen and your results go haywire. DateTime objects represent a *point* in time, not an arbitrary *span* of time. – Sammitch Oct 07 '15 at 20:01
  • so is it easier to add a date to my method, or yours? –  Oct 07 '15 at 20:03
  • `$dif1 = strtotime($time1)-strtotime($time2);` `$hours = intval( $dif1/(60*60));` `$mins = ($dif1-($hours*3600))/60;` @BalaMurugan – Subin Thomas Oct 07 '15 at 20:03
  • @Dagon, you should consider lifting the duplicate flag. – Rohit Gupta Oct 07 '15 at 20:04
  • nope, its totally a dupe, and there are a dozen ways to do this, like anything in php –  Oct 07 '15 at 20:05
  • @Dagon THERE IS NO DATE. This is "24 hours, 0 minutes MINUS 18 hours, 32 minutes", not "the difference between midnight and 6:32PM on a particular day". – Sammitch Oct 07 '15 at 20:06
  • im not sure what we are screaming about? but im going to work –  Oct 07 '15 at 20:06
  • 1
    Thank you Subin Thomas Its working Perfectly. Thank You Very Much – Bala Murugan Oct 07 '15 at 20:08
  • 1
    @Dagon if you're looking at a clock, it's a Date and Time and you can use the `DateTime` object/functions. If you're looking at a Stopwatch it's a duration and treating it as a DateTime [or similar functions like strtotime] can and will introduce errors in the calculation because of DST transitions and leap time adjustments. It will work 99.9% of the time, but it will break *badly* 0.01% of the time. – Sammitch Oct 07 '15 at 20:11
  • @Sammitch : Yes. there is a chance of break. In such case, we need to think of adding base year 1970 in conversion. `strtotime("1970-01-01 ". $time2);` . Duplicate flag was not necessary. – Subin Thomas Oct 07 '15 at 20:16
  • That's it, I give up. – Sammitch Oct 07 '15 at 20:33
  • use strtotime() for both time and then subtract both you will get the difference of both – Domain Oct 27 '15 at 05:06

0 Answers0