Jump to content

function help


will35010

Recommended Posts

I have this function that calculates time between two timestamps. I want it to display days and subtract that time from the hours. Example output would be 2 Day(s) & 3 Hour(s) &  43 Minutes

 

Right now it displays the days, but it displays it like 7.33333. I don't want anything past the decimal point and it also doesn't take away from the hour count.

//function to calculate time duration using timestamps
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); 
$days=floor($hours/24);

if($hours>=24) {
    $daysdisplay = ($hours/24);
}
if($hours==1) { 
$hoursdisplay=$hours; 
} 
if(($minutes%60) > 1) { 
$minutesdisplay=($minutes%60); 
}
else if(($minutes%60)==1) { 
$minutesdisplay=($minutes%60);
} 
else { $minutesdisplay=""; 
} 
$display=$daysdisplay."Day(s)&  ".$hours."Hour(s) &  ".$minutesdisplay." Mins"; 

return $display;
}

Link to comment
Share on other sites

Doesn't really seem like a very efficient 'time ago' script.

 

You can find a few alternatives on this page:

http://php.net/manual/en/function.time.php

 

Search for 'ago' :)

 

This one seems to be pretty good though:

http://aidanlister.com/2004/04/making-time-periods-readable/

 

This would be a proper way of using it:

function getTIME($time) {
$convert = time( ) - $time;

return time_duration($convert);
}

 

You could also tweak your own script to fit for this, but I wouldn't really recommend it because it's rudementary. You'd need to round ( ) them to the nearest integer.

Link to comment
Share on other sites

 

Fixed that. Thanks!

 

updated code

//function to calculate time duration using timestamps
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); 
$days=floor($hours/24);

if($hours>=24) {
    $daysdisplay = round($hours/24);
}
if($hours==1) { 
$hoursdisplay=$hours; 
} 
if(($minutes%60) > 1) { 
$minutesdisplay=($minutes%60); 
}
else if(($minutes%60)==1) { 
$minutesdisplay=($minutes%60);
} 
else { $minutesdisplay=""; 
} 
$display=$daysdisplay."Day(s)&  ".$hours."Hour(s) &  ".$minutesdisplay." Mins"; 

return $display;
}

Link to comment
Share on other sites

Doesn't really seem like a very efficient 'time ago' script.

 

You can find a few alternatives on this page:

http://php.net/manual/en/function.time.php

 

Search for 'ago' :)

 

This one seems to be pretty good though:

http://aidanlister.com/2004/04/making-time-periods-readable/

 

This would be a proper way of using it:

function getTIME($time) {
$convert = time( ) - $time;

return time_duration($convert);
}

 

You could also tweak your own script to fit for this, but I wouldn't really recommend it because it's rudementary. You'd need to round ( ) them to the nearest integer.

 

My script already does that.

Link to comment
Share on other sites

Don't double post.

 

Just copy a pre-fixed script. I'm not going to waste my time if there are plenty of scripts out there that fit your needs.

 

I didn't double post.

 

Don't waste your time since you can't help anyway. If I wanted to replace it, I would have just ran to Google and copied someone's code. I wanted to fix my code so I could learn from it. Forgive me for trying to actually learn vs. copying someone's code that I didn't write.

Link to comment
Share on other sites

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.