943,909 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 3565
  • PHP RSS
Sep 6th, 2008
0

time difference from time stamp

Expand Post »
Hi

I am trying to get the number of hours and minutes between
two time time stamps.

The expected results should be 1:59 (h:mm) instead I am getting 1:58 (h:mm).
What is wrong with my approach?

Is it also possible to use a built in php function so I don't have to
divide by 60 to convert to hours and minutes?

[php]
PHP Syntax (Toggle Plain Text)
  1. <?
  2.  
  3. $end_time = "2008-09-05 20:59:13";
  4. $start_time = "2008-09-05 19:00:16";
  5.  
  6. $end = date("h:i:s",strtotime($end_time));
  7. $start = date("h:i:s",strtotime($start_time));
  8. $diff = strtotime($end) - strtotime($start);
  9.  
  10. //convert to min and sec
  11. $convert_min = $diff/60;
  12. $convert_sec = $diff % 60;//seconds
  13.  
  14. //convert to hours and min
  15. $convert_hr = floor($convert_min/60);//hours
  16. $remainder = floor($convert_min % 60);//minutes
  17.  
  18. $total_visit = $convert_hr.":".$remainder;
  19.  
  20. ?>
  21. [/php]
Similar Threads
Reputation Points: 24
Solved Threads: 0
Junior Poster in Training
assgar is offline Offline
89 posts
since Oct 2006
Sep 6th, 2008
0

Re: time difference from time stamp

I am getting the correct hours and minutes by removing the seconds.
E.g. date("h:i",strtotime($end_time)).
I am still open to a more efficient way to accomplish this task.

[php]
PHP Syntax (Toggle Plain Text)
  1. <?
  2.  
  3. $end_time = "2008-09-05 20:59:13";
  4. $start_time = "2008-09-05 19:00:16";
  5.  
  6. $end = date("h:i",strtotime($end_time));
  7. $start = date("h:i",strtotime($start_time));
  8. $diff = strtotime($end) - strtotime($start);
  9.  
  10. //convert to min and sec
  11. $convert_min = $diff/60;
  12. $convert_sec = $diff % 60;//seconds
  13.  
  14. //convert to hours and min
  15. $convert_hr = floor($convert_min/60);//hours
  16. $remainder = floor($convert_min % 60);//minutes
  17.  
  18. $total_visit = $convert_hr.":".$remainder;
  19.  
  20. ?>
  21. [/php]
Reputation Points: 24
Solved Threads: 0
Junior Poster in Training
assgar is offline Offline
89 posts
since Oct 2006
Sep 6th, 2008
0

Re: time difference from time stamp

Check out the documentation on the date_sub function - I think this will do what you are after. Here is a link: http://au2.php.net/manual/en/function.date-sub.php
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Reading the stored procedure OUT parameter in PHP
Next Thread in PHP Forum Timeline: Calling Audio Conversion Tool with PHP on Win 2003





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC