時間戳轉換為幾分鍾、幾小時、幾天、幾周、幾月、幾年前


 1 <?php
 2 //時間戳轉換為幾分鍾、幾小時、幾天、幾周、幾月、幾年前
 3 function timeFormat($time)
 4 {
 5     $time = (int)substr($time, 0, 10);
 6     $int = time() - $time;
 7     $str = '';
 8     if ($int <= 30) {
 9         $str = sprintf('剛剛', $int);
10     } elseif ($int < 60) {
11         $str = sprintf('%d秒前', $int);
12     } elseif ($int < 3600) {
13         $str = sprintf('%d分鍾前', floor($int / 60));
14     } elseif ($int < 86400) {
15         $str = sprintf('%d小時前', floor($int / 3600));
16     } elseif ($int < 604800) {
17         $str = sprintf('%d天前', floor($int / 86400));
18     } elseif ($int < 2592000) {
19         $str = sprintf('%d周前', floor($int / 604800));
20     } elseif ($int < 31536000) {
21         $str = sprintf('%d月前', floor($int / 2592000));
22     } elseif ($int < 946080000) {
23         $str = sprintf('%d年前', floor($int / 31536000));
24     } else {
25         $str = date('Y-m-d H:i:s', $time);
26     }
27     return $str;
28 }
29 
30 echo timeFormat($time = 1564588800);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM