Current location: Hot Scripts Forums » Programming Languages » PHP » Difference in hours / minutes


Difference in hours / minutes

Reply
  #1 (permalink)  
Old 03-31-08, 03:10 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Difference in hours / minutes

I've searched all over and can't find any code that provides the difference between to sets of hours and minutes....here's what I'm trying to do....

For example, a user is supposed to get 50 hours and 30 minutes of account time:

50:30

But in fact he only gets 45 hours and 42 minutes:

45:42

How can I find the difference in in hours and minutes (that is, 4 hours and 18 minutes)?

I've looked at all sorts of stuff with strtotime, strfromtime, and about a billion lines of code examples but I've yet to find anything that simply subtracts one set of hours and minutes from another set of hours and minutes. I have a terrible feeling that this is really, really simple, but the problem is that *I'm* also really, really simple and I just can't see it.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
  #2 (permalink)  
Old 03-31-08, 03:40 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
You can actually use strtotime() (assuming you want to show a date/time so many hours:minutes relative to some starting/current date/time), however strtotime() does not directly accept the HH:MM (or HH:MM:SS) format in any math (+/-) statements. But if you break it up and do something like the following, it works -

PHP Code:

strtotime("now + 50 hours + 30 minutes"
Edit: Here is some code I just did for someone where the difference between two HH:MM were calculated -

PHP Code:

$from '08:31' 
list(
$hours,$minutes) = explode(":",$from);
$to '16:30' ;  
$output date("H:i",strtotime("$to - $hours hours - $minutes minutes"));
echo 
$output
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???

Last edited by mab; 03-31-08 at 03:50 PM.
Reply With Quote
  #3 (permalink)  
Old 03-31-08, 03:46 PM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
I'm guessin you could code it to work it out. Are you just hoping someone knows a quick way around it End User?
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Reply With Quote
  #4 (permalink)  
Old 03-31-08, 04:13 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Quote:
Originally Posted by mab View Post
Edit: Here is some code I just did for someone where the difference between two HH:MM were calculated -

PHP Code:

$from '08:31' 
list(
$hours,$minutes) = explode(":",$from);
$to '16:30' ;  
$output date("H:i",strtotime("$to - $hours hours - $minutes minutes"));
echo 
$output
Thanks, mab, I'll try this and see if I can get t to work- the one problem I'm running into (well, one of the problems, lol) is that either time can be larger or smaller...that is, the times could be like this, shown in time in hours and minutes:

50:35 (50 hours, 35 minutes scheduled time)
42:29 (42 hours, 29 minutes actual time)

Or they could be like this

47:16 (47 hours, 16 minutes scheduled time)
59:24 (59 hours, 24 minutes actual time)

The number are a count of the hours and minutes they were scheduled for and what they actually got, and the number they got can be more or less than they were scheduled to receive. (For example, they could be scheduled for 5 hours and only get 2 1/2, or they could be scheduled for 5 hours and actually get 12.)

So, I need to be able to find the difference, and whether it was positive or negative (did they more or less actual time than was scheduled).

I'll try this and see if I can get t to work.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
  #5 (permalink)  
Old 03-31-08, 04:14 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Quote:
Originally Posted by Jay6390 View Post
I'm guessin you could code it to work it out. Are you just hoping someone knows a quick way around it End User?
More like praying or groveling. I've been beating my head against the wall on this and the wall is starting to win.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
  #6 (permalink)  
Old 03-31-08, 04:32 PM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
Ok, well here is a very VERY messy but working script lol. you will need to clean it up a bit me thinks but u shud get the general idea of what it does

PHP Code:

<?php
$from 
'50:30' ;
list(
$hours,$minutes) = explode(":",$from);
$totalfrom $minutes +(60*$hours);
$to '45:42' ;
list(
$hours,$minutes) = explode(":",$to);
$totalto $minutes +(60*$hours);
$diff $totalfrom $totalto;

$neg = ($diff<0)?'-':'';
$diff abs($diff);
$diffhrs intval($diff 60);
$diffmins $diff &#37; 60;


echo "DIFFERENCE: {$neg}$diffhrs:$diffmins";

?>
Oh, and your original post, the difference is 4:48 not 4:18
Hopefully your head will start winning the competition now against the wall lol

EDIT: The $diffmins line should be $diffmins = $diff % 60 but for some reason its not parsing it correctly

Jay
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName

Last edited by Jay6390; 03-31-08 at 04:35 PM.
Reply With Quote
  #7 (permalink)  
Old 03-31-08, 07:34 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Thank you, Jay! I think this is very close to what I'm looking for. Let me play with this for a bit and I'll come back and report the current condition of the wall, lol.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
  #8 (permalink)  
Old 03-31-08, 07:40 PM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by End User View Post
Thank you, Jay! I think this is very close to what I'm looking for. Let me play with this for a bit and I'll come back and report the current condition of the wall, lol.
*at the ready with polyfiller*
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Reply With Quote
  #9 (permalink)  
Old 03-31-08, 10:15 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Quote:
Originally Posted by Jay6390 View Post
*at the ready with polyfiller*
Lol.

My head was just too fuzzed to think clearly, but that code worked and I thank you for it.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
  #10 (permalink)  
Old 04-01-08, 05:35 AM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by End User View Post
Lol.

My head was just too fuzzed to think clearly, but that code worked and I thank you for it.
hehe, no problem
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Determine Date within business hours omniman Script Requests 0 02-28-08 11:59 AM
date to "* hours ago" etc Mooooe PHP 2 09-09-06 01:39 AM
Total hours last month tbig ASP 5 09-03-05 09:40 PM
Time difference FiRe PHP 8 07-26-05 10:05 AM
Handy Banners ~ Prestige Quality Banners ~ 48 Hours Turn Around ~ Up To 8 Specials! barbesho Job Offers & Assistance 0 07-29-04 04:46 PM


All times are GMT -5. The time now is 07:39 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.