Last active
December 3, 2016 08:18
-
-
Save jk2K/6948915 to your computer and use it in GitHub Desktop.
更友好的显示时间
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function formattime($s) { | |
| if(date('Y', $s) < date('Y')) { | |
| return date('Y-n-d H:i', $s); | |
| } elseif(date('G', $s) > date('G')) { | |
| return date('n月d日 H:i', $s); | |
| } | |
| $limit = time() - $s; | |
| if($limit < 60) { | |
| return $limit . '秒前'; | |
| } elseif($limit >= 60 && $limit < 3600) { | |
| return floor($limit / 60) . '分钟前'; | |
| } elseif($limit >= 3600 && $limit < 86400) { | |
| return '今天' . date('H:i', $s); | |
| } elseif($limit >= 86400 && $limit < 31536000) { | |
| return date('n月d日 H:i', $s); | |
| } else { | |
| return date('Y-n-d H:i', $s); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| public static function get_simple_datetime($timestamp) | |
| { | |
| if (date('Y', $timestamp) < date('Y')) | |
| { | |
| return date('Y-n-d H:i', $timestamp); | |
| } | |
| elseif (date('G', $timestamp) > date('G')) | |
| { | |
| return date('n月d日 H:i', $timestamp); | |
| } | |
| $limit = time() - $timestamp; | |
| if ($limit < 3600) | |
| { | |
| return floor($limit / 60) . '分钟前'; | |
| } | |
| elseif ($limit >= 3600 && $limit < 86400) | |
| { | |
| return '今天' . date('H:i', $timestamp); | |
| } | |
| elseif ($limit >= 86400 && $limit < 604800) | |
| { | |
| // 1周内 | |
| return floor($limit / 86400) . '天前'; | |
| } | |
| elseif ($limit >= 604800 && $limit < 1209600) | |
| { | |
| return '1周前'; | |
| } | |
| elseif ($limit >= 1209600 && $limit < 1814400) | |
| { | |
| return '2周前'; | |
| } | |
| elseif ($limit >= 1814400 && $limit < 31536000) | |
| { | |
| // 1年内 | |
| return date('n月d日', $timestamp); | |
| } | |
| else | |
| { | |
| return date('Y-n-d', $timestamp); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment