LoginSignup
11
11

More than 5 years have passed since last update.

日時を、"6時間前", "たった今" など表示用文言に変換

Last updated at Posted at 2014-06-19
util.php
    /*
    *   日時を、"6時間前", "たった今" など表示用文言に変換
    */
    static function diffTime($date)
    {
        $time_diff = time() - strtotime($date);
        if ($time_diff < 60) {
            return 'たった今';
        } else if  ($time_diff < 600) {
            return floor($time_diff / 60) . '分前';           // 1~9分前
        } else if  ($time_diff < 3600) {
            return 5 * floor($time_diff / 300) . '分前';  // 10,15,20,25...55分前
        } else if  ($time_diff < 86400) {
            return floor($time_diff / 3600) . '時間前';      // 1~23時間前
        } else if  ($time_diff < 864000) {
            return floor($time_diff / 86400) . '日前';        // 1~9日前
        } else {
            return '10日以上前';
        }
    }
11
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
11