Jump to content

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;
}

I guess I didn't read the original post very well.

If you're not going to do the words Hours and Minutes you can make it even simpler.

$minutes=floor($time_period/60);
$hours=floor($minutes/60); 
echo $hours.":".($minutes%60);

 

 

 

 

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.