Jump to content

need help with function to calculate time


will35010

Recommended Posts

I have this function that I want to calculate the time difference. I want it to output in the format: HH:MM. Where do I go from where I'm at? Thanks!!!

 

function getTIME($time) {
## example start/end dates
$startdate = $time;
$enddate = time();

## difference between the two in seconds
$time_period = ( $enddate - $startdate );

this should be close

$minutes=floor($time_period/60); 
$hours=floor($minutes/60); 

if($hours==1) { $hoursdisplay=$hours." Hour "; } 
if(($minutes%60) > 1) { $minutesdisplay=($minutes%60)." Minutes"; 
else if(($minutes%60)==1) { $minutesdisplay=($minutes%60)." Minute"; 
else { $minutesdisplay=""; } 
$display=$hoursdisplay.$minutesdisplay; 

Ok. I got this far. It's displaying the total minutes, so what am I missing on my math? Also, is there a way to make it show only the whole number and leave off everything past the decimal point?

 

Thanks.

 

function getTIME($time) {
## example start/end dates
$startdate = $time;
$enddate = time();

## difference between the two in seconds
$time_period = ( $enddate - $startdate );

$day = '86400';
$hour = '3600';
$minute = '60';
$second = '1';

$days = ($time_period / $day);
$hours = ($time_period / $hour);
$minutes = ($time_period / $minute);
$seconds = ($time_period / $second);
echo $hours.":".$minutes;
}

I fixed the displaying past the decimal, but I cannot get it to display right. It's displaying like this: 21:1,273

 

function getTIME($time) {
## example start/end dates
$startdate = $time;
$enddate = time();

## difference between the two in seconds
$time_period = ( $enddate - $startdate );

$day = '86400';
$hour = '3600';
$minute = '60';
$second = '1';

$days = ($time_period / $day);
$hours = ($time_period / $hour);
$minutes = ($time_period / $minute);
$seconds = ($time_period / $second);
echo number_format($hours).":".number_format($minutes);
}

You could use the suggestion taquitosensei gave you.  ($minutes % 60) will return the number of minutes (between 0 and 59) with the hours, days, etc. stripped off.

 

But it you want all of the components (which you don't seem to be using) you have to subtract out each level as you go through:

function getTIME($time) {
   ## example start/end dates
$startdate = $time;
$enddate = time();

## difference between the two in seconds
$time_period = ( $enddate - $startdate );

$day = '86400';
$hour = '3600';
$minute = '60';
$second = '1';

$days = intval($time_period / $day);
$time_period -= ($days * $day);
$hours = ($time_period / $hour);
$time_period -= ($hours * $hour);
$minutes = ($time_period / $minute);
$time_period -= ($minutes * $minute);
$seconds = ($time_period / $second);
echo number_format($hours).":".number_format($minutes);
}

and I wouldn't use number_format, instead use: printf('%2d:%02d, $hours, $minutes)

 

or, the quickest way, might be

echo date($time_period, 'H:i');

 

I think that would work, haven't tried it.

I'm using this code now, but it keeps giving me a parse T_ELSE error for this line:

else if(($minutes%60)==1) { $minutesdisplay=($minutes%60)." Minute";

 

function getTIME($time) {
## example start/end dates
$startdate = $time;
$enddate = time();

## difference between the two in seconds
$time_period = ( $enddate - $startdate );

$minutes=floor($time_period/60); 
$hours=floor($minutes/60); 

if($hours==1) { $hoursdisplay=$hours." Hour "; } 
if(($minutes%60) > 1) { $minutesdisplay=($minutes%60)." Minutes"; 
else if(($minutes%60)==1) { $minutesdisplay=($minutes%60)." Minute"; 
else { $minutesdisplay=""; } 
$display=$hoursdisplay.$minutesdisplay; 

echo $display;
}

I think it's because I made a typo and forgot the closing bracket

 

Ok. Thanks for your help! I got it working with this, but it's not showing the hours. Did I add too many brackets?

 

function getTIME($time) {
## example start/end dates
$startdate = $time;
$enddate = time();

## difference between the two in seconds
$time_period = ( $enddate - $startdate );

$minutes=floor($time_period/60); 
$hours=floor($minutes/60); 

if($hours==1) { 
$hoursdisplay=$hours." Hour "; 
} 
if(($minutes%60) > 1) { 
$minutesdisplay=($minutes%60)." Minutes"; 
}
else if(($minutes%60)==1) { 
$minutesdisplay=($minutes%60)." Minute";
} 
else { $minutesdisplay=""; 
} 
$display=$hoursdisplay.$minutesdisplay; 

echo $display;
}

Fixed it by using this. Thanks for everybody's help!  :D

function getTIME($time) {
//start/end dates
$startdate = $time;
$enddate = time();

//difference between the two in seconds
$time_period = ( $enddate - $startdate );

$minutes=floor($time_period/60); 
$hours=floor($minutes/60); 

if($hours==1) { 
$hoursdisplay=$hours; 
} 
if(($minutes%60) > 1) { 
$minutesdisplay=($minutes%60); 
}
else if(($minutes%60)==1) { 
$minutesdisplay=($minutes%60);
} 
else { $minutesdisplay=""; 
} 
$display=$hours.":".$minutesdisplay; 

echo $display;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.