Jump to content

Calculate Total hours and Mins (over 24)


honkmaster

Recommended Posts

Hi I need to display total hours and mins as this example "35 Hrs 23 Mins", issue I'm having is after 24 it goes back to zero

I'm storing the time as follows 127380 (seconds) I should get 35:23 but I'm getting 59:23 ??? Any ideas would help

 

$date = '127380';
$minsec = gmdate("i:s", $date); 
$hours = gmdate("d", $date)*24 + gmdate("H", $date);
echo $time = $hours . ':' . $minsec;

 

Link to comment
Share on other sites

You are working to a formatted hour period which will only go up to 12 or 24 hour format, instead use a actual milliseconds value that equals an hour 3600

<?php

$date = '127380';

//$date = '259200'; // (3days) 72hours 0 mins

//$date = '259100'; // 71hrs 58mins

$hours = $date/3600;

echo (int) $hours.' Hrs '.gmdate('i',$date).' Mins<br>';

 

Link to comment
Share on other sites

I don't see a reason to use gmdate or date at all, this is just an arithmetic problem.

$date = 127380; // this is a number, do not quote it
	$seconds_per_minute = 60;
	$seconds_per_hour = 3600;
	 
	$hours = floor($date / $seconds_per_hour);
	$date = $date - ($hours * $seconds_per_hour);
	$minutes = floor($date / $seconds_per_minute);
	 
	echo $hours ' hours ' . $minutes . ' minutes';

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...