• 0

A better time display functionset


Question

Hey everyone. It's been awhile since I've been heavily active on Neowin, but I thought I'd post this here anyway. I couldn't find this particular set of functions online anywhere, so I wrote them myself. It untilizes epoch timestamps generated from the PHP time() function.

<?php
function CoolTimeRep($now, $compare){
$difference = $now - $compare;

$minutes = round($difference / 60);
$hours = round($difference / 3600);
$days = round($difference / 86400);
$weeks = round($difference / 604800);
$years = round($difference / 31449600);

	if($years != 0){return $years . " year".plural($years)." ago";}

	elseif($weeks != 0){return $weeks . " week".plural($weeks)." ago";}

	elseif($days != 0){return $days . " day".plural($days)." ago";}

	elseif($hours != 0){return $hours . " hour".plural($hours)." ago";}

	elseif($minutes != 0){return $minutes . " minute".plural($minutes)." ago";}

	else{return "just now";}

}

function plural($var){
if($var < "2"){$nothing;}
else{return "s";}
}
?>

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.