ソーシャルなサービスを支援 (PHP Advent Calendar 2010 14日目)
久しぶりの記事です。
今年に入ってだらだらと日々を過ごしてしまい記事を書けずにいましたが、このAdvent Calender 2010をきっかけに再び日課になれたらと思います。
ソーシャルなサービスを支援
少し前に作成して放置しているopenpearライブラリの紹介です。
http://openpear.org/package/Services_Twitter_Time
Exception処理とかしていないやっつけライブラリなのですが、意外と使用するので良い感じ。
(誰かメンテナンスしてくれると嬉しいです。)
ソースコード
<?php class Services_Twitter_Time { private $prefix = 'about '; private $now_time; private $conditions; public function __construct() { $this->now_time = time(); $this->conditions = array ( 1 => array( 'range' => range(1,50), 'word' => ' seconds ago' ), 60 => array( 'range' => range(1,59), 'word' => ' minutes ago' ), 3600 => array( 'range' => range(1,23), 'word' => ' hours ago' ), 86400 => array( 'range' => range(1,29), 'word' => ' days ago' ), 1036800 => array( 'range' => range(1,11), 'word' => ' months ago' ), 12441600 => array( 'range' => range(1,100), 'word' => ' years ago' ), ); } public function setNowTime ($now_time) { $this->now_time = $now_time; } public function get ($time) { foreach ($this->conditions as $key => $conditions) { foreach ($conditions['range'] as $range) { if ($this->now_time - $time <= (int)($range * $key)) { return $this->prefix . $range . $conditions['word']; } } } } }
使用方法
getメソッドにタイムスタンプを指定すれば、現在の時間(now_timeプロパティ)との差を計算して"Twitter風の時間表示に整形"して表示します。
<?php require_once('Services/Twitter/Time.php'); $time = new Serveices_Twitter_Time(); echo $time->get(...);// 6 minute ago等...
こんな感じです。
[PR]Spreeの情報を集めています。
ECを持ちたい方、仕事でECを使いたい方向けのコミュニティサイトです。
このサイトでは世界で最も使用されているECの1つであるSpreeについての情報を提供しています。
http://spreecommerce.jp/