我有一个帖子,我需要在我的成功函数中发布时间。对于 php,我的原始链接看起来像这样。
echo "<br/><a href='#' class='subtleLink' style='font-weight:normal;'>".Agotime($streamitem_data['streamitem_timestamp'])."</a>";
我正在构建我的 ajax,在我的成功函数中,我还需要添加 Agotime,但我不知道该怎么做,我有时间戳,只需要函数 Agotime 来完成它的工作。
AJAX
<br/><a class='subtleLink' style='font-weight:normal;'>"+response['streamitem_timestamp']+"</a>
我的 Agotime 是一个函数
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}";
}
?>