簡體   English   中英

在PHP中將時間戳轉換為時間

[英]Converting timestamp to time ago in PHP

我有這個時間戳的PHP代碼,不會顯示正確的時間。 它總是顯示在8小時前,而時間應該是在幾分鍾前。

  /** * Convert a timestap into timeago format * @param time * @return timeago */ public static function timeago($time, $tense = "ago"){ if(empty($time)) return "n/a"; $time=strtotime($time); $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade"); $lengths = array("60","60","24","7","4.35","12","10"); $now = time(); $difference = $now - $time; 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 "; } 

我真的不明白為什么它不能正常工作

這是我為您編寫的函數。

它使用DateTime類 ,因此PHP版本必須> = 5.3.0。

閱讀函數中的注釋以了解其工作原理。

function timeago($time, $tense='ago') {
    // declaring periods as static function var for future use
    static $periods = array('year', 'month', 'day', 'hour', 'minute', 'second');

    // checking time format
    if(!(strtotime($time)>0)) {
        return trigger_error("Wrong time format: '$time'", E_USER_ERROR);
    }

    // getting diff between now and time
    $now  = new DateTime('now');
    $time = new DateTime($time);
    $diff = $now->diff($time)->format('%y %m %d %h %i %s');
    // combining diff with periods
    $diff = explode(' ', $diff);
    $diff = array_combine($periods, $diff);
    // filtering zero periods from diff
    $diff = array_filter($diff);
    // getting first period and value
    $period = key($diff);
    $value  = current($diff);

    // if input time was equal now, value will be 0, so checking it
    if(!$value) {
        $period = 'seconds';
        $value  = 0;
    } else {
        // converting days to weeks
        if($period=='day' && $value>=7) {
            $period = 'week';
            $value  = floor($value/7);
        }
        // adding 's' to period for human readability
        if($value>1) {
            $period .= 's';
        }
    }

    // returning timeago
    return "$value $period $tense";
}

不要忘記設置您的工作時區

date_default_timezone_set('UTC');

用它

echo timeago('1981-06-07'); // 34 years ago
echo timeago(date('Y-m-d H:i:s')); // 0 seconds ago

等等

可能您的時區設置不正確! 在獲取時間之前,請嘗試設置默認時區:

date_default_timezone_set ( string $timezone_identifier )

有關PHP.net上的函數的更多信息

支持的時區列表

function time_elapsed($datetime, $full = false) {

$now = time();
$ago = strtotime($datetime);

$diff   = $now - $ago; 

$string = array(
    'year'  => 31104000,
    'month' => 2592000,
    'week'  => 604800,
    'day'   => 86400,
    'hour'  => 3600,
    'minute'=> 60,
    'second'=>  1
);

$data = array();

foreach ($string as $k => $v) {

    if($diff > $v){
        $count    = round($diff / $v);
        $data[$k] = $count . (($count > 1) ? ' ' . $k .'s' : ' ' . $k);
        $diff     = $diff % $v;
    }
}

if (!$full) $data = array_slice($data, 0, 1);
   return $data ? implode(', ', $data) . ' ago' : 'just now';
}

echo time_elapsed('2016-01-18 13:07:30', true); 
      // 2 years, 1 month, 2 weeks, 6 days, 25 seconds ago
echo time_elapsed('2016-01-18 13:07:30'); 
     // 2 years ago

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM