在php中的timeAgoFormat转换中显示负值

时间:2014-02-10 15:42:37

标签: php

我遇到的情况是timeAgoFormat显示结果为负值。喜欢-18000秒前。但当我上传到在线服务器时,它显示正确的结果。比如1秒前。为什么? 我的代码如下。

`

class convertToAgo {

function convert_datetime($str)
{
    list($date, $time) = explode(' ', $str);
    list($year, $month, $day) = explode('-', $date);
    list($hour, $minute, $second) = explode(':', $time);
    $timestamp = mktime($hour, $minute, $second, $month, $day, $year);

    return $timestamp;
}

function makeAgo($timestamp)
{
    $difference = time() - $timestamp;

    $periods = array("sec", "min", "hr", "day", "week", "month", "year", "decade");
    $lengths = array("60","60","24","7","4.35","12","1","10");

    $text = '';
    for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++)
    {
        $difference /= $lengths[$j];
    }

    $difference = round($difference);

    if($difference != 1)
    {
        $periods[$j].= "s";
    }

    $text = "$difference $periods[$j] ago<br />";

    return $text;
}

}

?&gt;`

可靠定义
$ myObject = new convertToAgo;

$postdate = $row["postdate"];
$convertedTime = ($myObject -> convert_datetime($postdate));
$whenPost = ($myObject -> makeAgo($convertedTime));

我正在回复$ whenPost

1 个答案:

答案 0 :(得分:0)

根据您在评论中所说的内容,尝试添加:

date_default_timezone_set('Asia/Calcutta');

到你的脚本顶部,看看会发生什么。