2010-12-12 10 views
1

UNIXのタイムスタンプの違いのための良い印刷ライブラリがありますか? 「x分前」、「x時間前」、「x日前」などのようなソーシャルネットワーキングサイトに見られるもののようなものに何か。私は自分自身で書くのは難しくありませんが、なぜホイールを再発明するのでしょうか?あなたが必要とするものの、PHPの時差のためのきれいな印刷ライブラリ

DateInterval

はあなたが希望する出力を得るためにformat()メソッドを使用します。

+0

私はこれを最近他の人に返しました:http://stackoverflow.com/questions/4394161/php-time-calculation/4394181#4394181 –

答えて

1

これはPHPのライブラリがあるのか​​どうかわかりませんが、Jeff(このWebサイトを作成した人)はこの質問を長い間前に聞いていました。詳細は、this questionを参照してください。私はあなたがそれから多くのインスピレーションを得ることができると確信しています。ジェフはその質問にthe code they use on StackOverflow itselfで答えます。

私はこれを自分で書くのは難しいとは思わないので、図書館を探して30分ではなく5分を書いてみませんか?

2

<?php 
function rel_time($from, $to = null) 
{ 
    $to = (($to === null) ? (time()) : ($to)); 
    $to = ((is_int($to)) ? ($to) : (strtotime($to))); 
    $from = ((is_int($from)) ? ($from) : (strtotime($from))); 

    $units = array 
    (
    "year" => 29030400, // seconds in a year (12 months) 
    "month" => 2419200, // seconds in a month (4 weeks) 
    "week" => 604800, // seconds in a week (7 days) 
    "day" => 86400, // seconds in a day (24 hours) 
    "hour" => 3600,  // seconds in an hour (60 minutes) 
    "minute" => 60,  // seconds in a minute (60 seconds) 
    "second" => 1   // 1 second 
); 

    $diff = abs($from - $to); 
    $suffix = (($from > $to) ? ("from now") : ("ago")); 

    foreach($units as $unit => $mult) 
    if($diff >= $mult) 
    { 
    $and = (($mult != 1) ? ("") : ("and ")); 
    $output .= ", ".$and.intval($diff/$mult)." ".$unit.((intval($diff/$mult) == 1) ? ("") : ("s")); 
    $diff -= intval($diff/$mult) * $mult; 
    } 
    $output .= " ".$suffix; 
    $output = substr($output, strlen(", ")); 

    return $output; 
} 
?> 
0

PHPがビルトインこのため以降PHP 5.3からクラスを持ってしてみてください私が思う必要なユニットを決定する。

0
defined('SECOND') ? NULL : define('SECOND', 1); 
defined('MINUTE') ? NULL : define('MINUTE', 60 * SECOND); 
defined('HOUR') ? NULL : define('HOUR', 60 * MINUTE); 
defined('DAY') ? NULL : define('DAY', 24 * HOUR); 
defined('MONTH') ? NULL : define('MONTH', 30 * DAY); 
defined('YEAR') ? NULL : define('YEAR', 12 * MONTH); 

class Time{ 
    public $td; 

    public function set_td($timestamp){ 
     $this->td = time() - $timestamp; 
    } 

    public function string_time($timestamp){ 

     $this->set_td($timestamp); 


     if ($this->td < 0){ 
      return "not yet"; 
     } 
     if ($this->td < 1 * MINUTE){ 
      return $this->td <= 1 ? "just now" : $this->td." seconds ago"; 
     } 
     if ($this->td < 2 * MINUTE){ 
      return "a minute ago"; 
     } 
     if ($this->td < 45 * MINUTE){ 
      return floor($this->td/MINUTE)." minutes ago"; 
     } 
     if ($this->td < 90 * MINUTE){ 
      return "an hour ago"; 
     } 
     if ($this->td < 24 * HOUR){ 
      return floor($this->td/HOUR)." hours ago"; 
     } 
     if ($this->td < 48 * HOUR){ 
      return "yesterday"; 
     } 
     if ($this->td < 30 * DAY){ 
      return floor($this->td/DAY)." days ago"; 
     } 
     if ($this->td < 12 * MONTH){ 
      $months = floor($this->td/MONTH); 
      return $months <= 1 ? "one month ago" : $months." months ago"; 
     }else{ 
      $years = floor($this->td/YEAR); 
      return $years <= 1 ? "one year ago" : $years." years ago"; 
     } 
    } 
} 

$time = new Time(); 

あなたのタイムスタンプを新しいオブジェクトに入れて、時間差diffenreceを秒で入力してください。

$string = $time->string_time($timestamp); 
関連する問題