CoderZone.org
Pages: 1 « previous     next »
  Print  
Author Topic: Minutes between two dates  (Read 5713 times) Bookmark and Share
Flynn
Newbie
*
Posts: 9



View Profile
« on: Jan 14, 2011, 08:37:00 am »

I'm having an attack of The Stupids. This should be simple but I'm borking it up somewhere.

I have two dates and I'm trying to find the minutes between the dates, but only within a given month. Take these two dates:

2010-12-27 14:05:00  (Dec 27 @ 2:05 pm)
2011-01-03 17:40:00  (Jan 3 @ 5:40 pm)

I only want the time that's in January, so I clip off days before the start of the month for the first date and turn it into "2011-01-01 00:00:00"...and this is where I think I'm going wrong.

Manually counting the time for "2011-01-01 00:00:00" to "2011-01-03 17:40:00" should be 48 hours for the first two days, plus 17 hours and 40 minutes for the partial 3rd day, or 65 hours and 40 minutes, right?

48 + 17 = 65 hours, plus 40 minutes.

The problem is all of the code I've tried reports 66 hours and 40 minutes, like this function:

Code:  
Highlight Mode: (PHP)
  1. function dateDiff($start, $end) {
  2. $start_time=strtotime($start);
  3. $end_time=strtotime($end);
  4. $diff = round(abs($end_time - $start_time) / 60, 2);
  5. return $diff;
  6. }
 

Several other functions give the same results: 66 hours and 40 minutes. Where oh where am I going wrong??
Logged
UnrealEd
Newbie
*
Posts: 22



View Profile
« Reply #1 on: Jan 14, 2011, 09:56:40 am »

I tested in my cmd window, and it returned 65 hours and 40 minutes (3940minutes)

How do you recalculate the hours and minutes based on the returned result, maybe the problem is situated there?
« Last Edit: Jan 14, 2011, 10:23:31 am by UnrealEd » Logged
phpMan2010
Newbie
*
Posts: 32



View Profile
« Reply #2 on: Jan 14, 2011, 10:13:43 am »

Sounds like a rounding error, after the results are returned, like Ed mentioned ...

Code:  
Highlight Mode: (PHP)
  1. $start='2011-01-01 00:00:00';
  2. $end='2011-01-03 17:40:00';
  3. $start_time=strtotime($start);
  4. $end_time=strtotime($end);
  5. $total_seconds=abs($end_time-$start_time);
  6. $seconds = (int)($total_seconds % 60);
  7. $minutes = (int)(($total_seconds / 60) % 60);
  8. $hours = (int)($total_seconds / 3600);
  9. echo 'Integers:'.PHP_EOL;
  10. echo $hours.':'.$minutes.':'.$seconds.PHP_EOL;
  11. $seconds = round($total_seconds % 60);
  12. $minutes = round(($total_seconds / 60) % 60);
  13. $hours = round($total_seconds / 3600);
  14. echo 'Floats:'.PHP_EOL;
  15. echo $hours.':'.$minutes.':'.$seconds.PHP_EOL;
  16. $seconds = floor($total_seconds % 60);
  17. $minutes = floor(($total_seconds / 60) % 60);
  18. $hours = floor($total_seconds / 3600);
  19. echo 'Floats - with floor:'.PHP_EOL;
  20. echo $hours.':'.$minutes.':'.$seconds.PHP_EOL;
 

Output
Quote
Integers:
65:40:0
Floats:
66:40:0
Floats - with floor:
65:40:0
Logged
Flynn
Newbie
*
Posts: 9



View Profile
« Reply #3 on: Jan 14, 2011, 01:15:40 pm »

I tested in my cmd window, and it returned 65 hours and 40 minutes (3940minutes)

How do you recalculate the hours and minutes based on the returned result, maybe the problem is situated there?

Your answer helped me find it by confirming that the code *did* work...so I *knew* it had to be elsewhere. Sure enough, I had swapped two vars in an earlier revision and one of them was being totaled cumulatively. One event was an hour long and was being quietly added to the result.

Thank you for the clarity injection, lol.
« Last Edit: Jan 14, 2011, 01:24:08 pm by Flynn » Logged
Flynn
Newbie
*
Posts: 9



View Profile
« Reply #4 on: Jan 14, 2011, 01:17:10 pm »

Your answer and Unreal Ed's answer convinced me that the problem wasn't in the place I was looking, and I ended up finding it. Thank you. (I wish this board had a "Thank You" button, I rather expect I'd be using it a lot.)
Logged
Tags:
Pages: 1
  Print  
 
Jump to: