I have my Agotime function set up. I’m just wondering what method I should use to make my time update live. Many would say Ajax but would this be server heavy? I need a smooth change every 5 seconds.
Also how could I do this?
function Agotime($date)
{
if(empty($date)) {
return "No date provided";
}
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
$now = time();
$unix_date = strtotime($date);
// check validity of date
if(empty($unix_date)) {
return "Bad date";
}
// is it future date or past date
if($now > $unix_date) {
$difference = $now - $unix_date;
$tense = "ago";
} else {
$difference = $unix_date - $now;
$tense = "from now";
}
for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
$difference /= $lengths[$j];
}
$difference = round($difference);
if($difference != 1) {
$periods[$j].= "s";
}
return "$difference $periods[$j] {$tense}";
}
You could implement it one of two ways.
You could use AJAX, as you proposed, and query your PHP script for how long ago it was. This could be heavy on the number of requests, however.
You could also implement the function in Javascript, and simply pass include pass the timestamp to the script on load. This way you won’t have to do any requests to the server, and could keep the time updated live. This, of course, assumes that the timestamp doesn’t change.
I’d personally pick option #2, as it’s easily calculated in Javascript, and avoids a lot of network traffic.