php中的时间戳错误

时间:2016-12-22 20:46:09

标签: php

我是PHP新手。我在下面的时间戳代码中得到时差。

public function time_ago($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}";
}

即使我现在发帖,我也会得到5小时的结果。我想要刚才或45秒前或1分钟前的结果。

任何人都可以帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

您必须设置您所在位置的时区。

Data(contentsOf: URL)