After a time searching the internet, I couldn’t find a good function to return “human” elapsed time, so I wrote one. I’m sharing it with you in hopes that you’ll find it as useful as I did. This will give you a result that ends up looking very similar to the way Twitter shows time elapsed, and exactly the way I display it on AnonTwit. Syntax highlighted code available at http://pastebin.ca/1466334 for those interested.
function elapsed_time ( $start, $end = false)
{
if ($end == false) //if you don't pass a second parameter,
$end = time(); //we assume you're comparing to now.
$diff = $end - $start;
$days = floor ( $diff/86400 ); //calculate the days
$diff = $diff - ($days*86400); // subtract the days
$hours = floor ( $diff/3600 ); // calculate the hours
$diff = $diff - ($hours*3600); // subtract the hours
$mins = floor ( $diff/60 ); // calculate the minutes
$diff = $diff - ($mins*60); // subtract the mins
$secs = $diff; // what's left is the seconds;
if ($secs > 0) {
$returnval = "$secs second".(($secs>1) ? "s":"")." ago";
}
if ($mins > 0) {
$returnval = "$mins minute".(($mins>1) ? "s":"")." ago";
}
if ($hours > 0) {
$returnval = "$hours hour".(($hours>1) ? "s":"")." ago";
}
if ($days > 0) {
$returnval = "$days day".(($days>1) ? "s":"")." ago";
}
$return = "About "; //change the wording to what you like
return $return . $returnval;
}
A much better function:
http://pastebin.ca/1466344
[edited by dcostalis for brevity: moved function to pastebin.ca]
Nothing at all wrong with that function, but I usually keep the functions I post on here relatively simple to understand for those who are learning.
Besides, I didn’t need all that when I wrote it!
Thanks for the post though, I’ll keep that in my bag of tricks for when I do have a use for it!