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
https://forums.phpfreaks.com/topic/210223-function-help/
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
https://forums.phpfreaks.com/topic/210223-function-help/#findComment-1097051
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
https://forums.phpfreaks.com/topic/210223-function-help/#findComment-1097052
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
https://forums.phpfreaks.com/topic/210223-function-help/#findComment-1097053
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
https://forums.phpfreaks.com/topic/210223-function-help/#findComment-1097105
Share on other sites

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.