0

I have two variables:

$start_time = '9:36';
$end_time = '12:47';

How would i calculate the amount of hours between the two times and store them in a variable like this:

$total_hours = 3;

I have read that you need to convert the times first. Also there will be no need for date as this will all be on the same day.

Any help would be appreciated

TehShrike
  • 9,855
  • 2
  • 33
  • 28
SebastianOpperman
  • 6,988
  • 6
  • 30
  • 36
  • Please see strtotime and date http://php.net/manual/en/function.strtotime.php and http://php.net/manual/en/function.date.php – Mob Nov 09 '11 at 07:10
  • See http://stackoverflow.com/questions/365191/how-to-get-time-difference-in-minutes-in-php/365214#365214 but divide by 60*60 instead. – bernie Nov 09 '11 at 07:11
  • @bernie: This is a little different case - you do not have full timestamps, you only have strings representing hours and minutes. – Tadeck Nov 09 '11 at 08:17

5 Answers5

2

if you only need to calculate the hours difference you could use the datetime class especifically the function date_parse_from_format where you just need to provide the format as seed and then what you need to parse give that calculate the difference

Edit 1

you could do something with less overhead:

$start_time = '9:36';
$end_time = '12:47';
$today = date('Y-d-m');
$start = strtotime($today . ' ' . $start_time);
$end = strtotime($today . ' ' . $end_time);

$diff = floor(($end - $start) / 3600);

// or if you don't need the exact hours

$diff = ($end - $start) / 3600;
Punit
  • 1,110
  • 1
  • 8
  • 14
Gabriel Sosa
  • 7,897
  • 4
  • 38
  • 48
2
  1. Convert time string tor timestamp: strftime
  2. Take the difference, and
  3. a. Use date to get the hours (H) value
    b. Divide with 3600 and round as much digit as you need

$total_hours = (int)date('G', abs(strtotime($time2)-strtotime($time1)));

netmano
  • 322
  • 1
  • 13
  • 1
    It works, except leaves zero in front of hour like that: "`03`". See [this codepad](http://codepad.org/EQSjrztD). – Tadeck Nov 09 '11 at 07:16
  • @Tadeck: To leave the leading zero, use `G` instead of `H` in `date`. `date('G', abs(strtotime($time2)-strtotime($time1)));`. And this works if more detailed times got as input, eg: `10:23:43 GMT`, as `strftime` could do the job – netmano Nov 09 '11 at 10:08
  • @netmano: So your solution should be rather `date('G', abs(strtotime($time2)-strtotime($time1)));`, not the one you have given. Or even this should be: `$total_hours = (int)date('G', abs(strtotime($time2)-strtotime($time1)));`, don't you think? – Tadeck Nov 09 '11 at 10:57
  • @Tadeck: yes, I missed the edit function, so I have written to commeny, btw, now I corrected in the answer. Thanks! And if you use the (int) casting, there is no problem of `H`, because 03 will be casted to 3 as int. – netmano Nov 09 '11 at 11:33
  • @netmano: Yes, in case of casting there is no difference in result. But to be consistent with different languages, having to chose between converting `03` and `3` to integer, I would choose the latter. In other languages ([such as eg. JavaScript](http://jsfiddle.net/xtsqb/)) the zero on the first position in numeric string matters. – Tadeck Nov 09 '11 at 11:39
1

See below

$start_time = '9:36';
$end_time = '12:47';

$v =  strtotime($end_time) - strtotime($start_time);
echo date("h:i", $v);

Outputs

 03:11

http://php.net/manual/en/function.strtotime.php and http://php.net/manual/en/function.date.php

Mob
  • 10,958
  • 6
  • 41
  • 58
0

Why are you guys making it complicated? It can be done in a one-liner code:

abs((strtotime("11:00 AM") - strtotime("12:00 PM")) / 3600)

Result:

1

Please note that this assumes that both time are in the same day, although it will still work if both are from different dates.

Norielle Cruz
  • 170
  • 5
  • 14
0

This solves your problem:

$start = explode(':', $start_time);
$end = explode(':', $end_time);
$total_hours = $end[0] - $start[0] - ($end[1] < $start[1]);

See this codepad for a proof.

This solution assumes both hours are from the same day.

Tadeck
  • 132,510
  • 28
  • 152
  • 198
  • Hi, i see how you used the explode function. It works. I subtracted the start date from the end date. the hours came out fine but it always miscalculated the minutes. Thank you – SebastianOpperman Nov 09 '11 at 07:23
  • @SebastianOpperman: No problem. Just know that if start and end are on different dates, this solution will not work properly and you will have to adjust it. Good luck. – Tadeck Nov 09 '11 at 08:09
  • just stupid question, is 10 - (true) safe to assume it will always be same as (10 - 1)? – Grzegorz Feb 24 '14 at 07:54
  • hi there @Tadeck, i tried your solution but it isn't really working i'm using 24 hour format and here's the result http://codepad.org/SxpWzo8A , but it doesn't even work with am/pm since maybe we aren't telling if its am/pm ? could you help it please ? – Habib Rehman Nov 13 '16 at 11:55