# [RESOLVED] Time Difference Between Two Times

• 12-15-2009, 09:31 AM
Chris Jacks
[RESOLVED] Time Difference Between Two Times
Hi all,

I'm pulling two times out of a MySQL database. The times are in the following format: HH:MM:SS (24-hour time including preleading zeros).

I want to be able to find the difference between the two times, and show it in the same format as per the examples below:

Example 1
Time1 = 08:00:00 (time is 8:00am)
Time2 = 13:40:00 (time is 1:40pm)
TimeDifference = 05:40:00 (difference of 5hours 40minutes)

Example 2
Time 1 = 22:00:00 (time is 10:00pm)
Time 2 = 04:00:00 (time is 4:00am the next morning)
TimeDifference = 06:00:00 (difference of 6hours)

"Time 1" will always be in the past and "Time 2" will always be in the future. As you can see, it's possible that "Time 2" will sometimes be an "earlier" time than "Time 1". In these cases, this will always mean that Time 2 is some time in the next day.

Between the many posts I've found here and also via BigResource, I haven't yet found a solution for this! Nor have any of my newbie efforts been any good.

If someone could show me how to do this, that would be great!

Cheers,

Chris
• 12-15-2009, 12:35 PM
chris22
You can maybe try code below:

PHP Code:

``` // example 1 \$time1 = "08:00:00"; \$time2 = "13:40:00"; echo "Time difference: ".get_time_difference(\$time1, \$time2)." hours<br/>"; // example 2 \$time1 = "22:00:00"; \$time2 = "04:00:00"; echo "Time difference: ".get_time_difference(\$time1, \$time2)." hours<br/>"; function get_time_difference(\$time1, \$time2) {     \$time1 = strtotime("1/1/1980 \$time1");     \$time2 = strtotime("1/1/1980 \$time2");          if (\$time2 < \$time1)     {         \$time2 = \$time2 + 86400;     }          return (\$time2 - \$time1) / 3600;      }  ```
• 12-15-2009, 04:52 PM
Mindzai
A slight modification of the above which returns the time in HH:MM:SS format:

PHP Code:

``` <?phpfunction get_time_difference(\$time1, \$time2) {    \$time1 = strtotime("1980-01-01 \$time1");    \$time2 = strtotime("1980-01-01 \$time2");        if (\$time2 < \$time1) {        \$time2 += 86400;    }        return date("H:i:s", strtotime("1980-01-01 00:00:00") + (\$time2 - \$time1));}echo get_time_difference("10:25:30", "22:40:59"); // 12:15:29?> ```
• 12-16-2009, 07:35 AM
Chris Jacks
Chris22 and Mindzai... THANKS VERY MUCH!!!

Both methods freaking brilliant!

if (\$time2 < \$time1) {
\$time2 += 86400;
}

(crazy simple now it's in front of me)

Chris
• 03-21-2013, 04:31 AM
pragnesh.karia
Nice Post..!
Nice Post..!!

Quote:

Originally Posted by chris22
You can maybe try code below:
Nice Post..!!

PHP Code:

``` // example 1 \$time1 = "08:00:00"; \$time2 = "13:40:00"; echo "Time difference: ".get_time_difference(\$time1, \$time2)." hours<br/>"; // example 2 \$time1 = "22:00:00"; \$time2 = "04:00:00"; echo "Time difference: ".get_time_difference(\$time1, \$time2)." hours<br/>"; function get_time_difference(\$time1, \$time2) {     \$time1 = strtotime("1/1/1980 \$time1");     \$time2 = strtotime("1/1/1980 \$time2");          if (\$time2 < \$time1)     {         \$time2 = \$time2 + 86400;     }          return (\$time2 - \$time1) / 3600;      }  ```

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.08136 seconds
• Memory Usage 2,394KB
• Queries Executed 11 (?)
Template Usage (21):
• (3)bbcode_php_printable
• (1)bbcode_quote_printable
• (1)footer
• (1)gobutton
• (1)navbar_moderation
• (1)navbar_noticebit
• (2)option
• (1)spacer_close
• (1)spacer_open

Phrase Groups Available (3):
• global
• postbit
Included Files (19):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/class_bbcode_alt.php
• ./includes/class_bbcode.php
• ./includes/functions_bigthree.php
• ./includes/functions_notice.php

Hooks Called (41):
• init_startup
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• bbcode_fetch_tags
• bbcode_create
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates