0

我有一个帖子,我需要在我的成功函数中发布时间。对于 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}";
}
?>
4

1 回答 1

0

我已经想通了。当我的 Ajax 调用时,我将 Agotime 添加到我的插入页面中。很有魅力。

$json = array();
$check = "SELECT streamitem_id, streamitem_timestamp FROM streamdata";
$check1 = mysql_query($check);
$resultArr = mysql_fetch_array($check1);
$json['streamitem_id'] = $resultArr['streamitem_id'];
$json['streamitem_timestamp'] = Agotime($resultArr['streamitem_timestamp']);
于 2012-08-03T09:42:04.313 回答