time difference from time stamp

Reply

Join Date: Oct 2006
Posts: 82
Reputation: assgar is an unknown quantity at this point 
Solved Threads: 0
assgar assgar is offline Offline
Junior Poster in Training

time difference from time stamp

 
0
  #1
Sep 6th, 2008
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]
  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]
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 82
Reputation: assgar is an unknown quantity at this point 
Solved Threads: 0
assgar assgar is offline Offline
Junior Poster in Training

Re: time difference from time stamp

 
0
  #2
Sep 6th, 2008
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]
  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]
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 793
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: time difference from time stamp

 
0
  #3
Sep 6th, 2008
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
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC