Jump to content

Recommended Posts

 

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?

 

[code]
<?

$end_time = "2008-09-05 20:59:13";
$start_time = "2008-09-05 19:00:16";

$end = date("h:i:s",strtotime($end_time));
$start = date("h:i:s",strtotime($start_time));
$diff = strtotime($end)  - strtotime($start);
  
//convert to min and sec
$convert_min = $diff/60;
$convert_sec = $diff % 60;//seconds

//convert to hours and min
$convert_hr = floor($convert_min/60);//hours
$remainder = floor($convert_min % 60);//minutes

$total_visit = 	$convert_hr.":".$remainder;	

?>

[/code]

Link to comment
https://forums.phpfreaks.com/topic/123040-time-difference-from-time-stamp/
Share on other sites

Hi

 

Thanks for responding.

 

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.

 

[code]
<?

$end_time = "2008-09-05 20:59:13";
$start_time = "2008-09-05 19:00:16";

$end = date("h:i",strtotime($end_time));
$start = date("h:i",strtotime($start_time));
$diff = strtotime($end)  - strtotime($start);
  
//convert to min and sec
$convert_min = $diff/60;
$convert_sec = $diff % 60;//seconds

//convert to hours and min
$convert_hr = floor($convert_min/60);//hours
$remainder = floor($convert_min % 60);//minutes

$total_visit = 	$convert_hr.":".$remainder;	

?>

[/code]

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.