Home >Backend Development >PHP Tutorial >The PHP version implements a friendly time display method (for example: 2 hours ago)

The PHP version implements a friendly time display method (for example: 2 hours ago)

WBOY
WBOYOriginal
2016-07-25 09:10:591243browse

Complete php class, usually I use it with smary for quick use (plugins/function.rdate.php)

  1. /*
  2. * Data time functions.
  3. // * 模块
  4. */
  5. defined('TSKY') || die('Permission Denied!');
  6. function fmtMonth($month){
  7. return date('F, Y',day2time($month.'01'));
  8. }
  9. //
  10. function fmt_month($ts) {
  11. return strftime("%b,%Y",$ts);
  12. }
  13. // 03:02
  14. function shartTime($ts) {
  15. return strftime("%H:%M",$ts);
  16. }
  17. // 03:02:01
  18. function longTime($ts) {
  19. return strftime("%T",$ts);
  20. }
  21. //4月18日
  22. function shortDate($ts) {
  23. return date("n月d日",$ts);
  24. }
  25. //2006年4月18日
  26. function longDate($ts) {
  27. return date("Y年n月d日",$ts);
  28. }
  29. function dateTime($ts) {
  30. return date("Y年n月d日 H:i:s",$ts);
  31. }
  32. function fullDateTime($ts) {
  33. return date("Y年n月d日 ",$ts).week($ts);
  34. }
  35. function week($ts) {
  36. global $lang;
  37. return $lang['weekDay'][date('w',$ts)];
  38. }
  39. function relatively_date($date) {
  40. if (!preg_match('/^d+$/', $date)) $date = strtotime(trim($date));
  41. $sec = time() - $date;
  42. switch(true){
  43. case $sec < 3600:
  44. return round($sec/60). ' 分钟前';
  45. case $sec < 86400:
  46. return round($sec/3600). ' 小时前';
  47. case $sec < (86400 * 7):
  48. return round($sec/86400). ' 天前';//days ago
  49. case $sec < (86400 * 7 * 4):
  50. return round($sec/(86400*7)). ' 周前'; //weeks ago
  51. default:
  52. return longDate($date);
  53. }
  54. }
  55. function nextMonth($month/*200512->200601*/){
  56. return date('Ym',strtotime('+1 month',strtotime($month.'01')));
  57. }
  58. function prevMonth($month/*200512->200511*/){
  59. return date('Ym',strtotime('-1 month',strtotime($month.'01')));
  60. }
  61. function prevDay($day/*20050826*/){
  62. $day = substr($day,0,8);
  63. return date('Ymd',strtotime('-1 day',strtotime($day)));
  64. }
  65. function nextDay($day/*20050826*/){
  66. $day = substr($day,0,8);
  67. return date('Ymd',strtotime('+1 day',strtotime($day)));
  68. }
  69. function nextExistsDay($day/*20050109*/){
  70. $day = nextDay($day);
  71. while(!hasTopic($day) && $day < TODAY){
  72. $day = nextDay($day);
  73. }
  74. return hasTopic($day) ? $day : false;
  75. }
  76. function prevExistsDay($day/*20050109*/){
  77. global $cfg;
  78. $day = prevDay($day);
  79. while(!hasTopic($day) && (int)$day > $cfg->origDate){
  80. $day = prevDay($day);
  81. }
  82. return hasTopic($day) ? $day : false;
  83. }
  84. function prev_day($day){$day = substr($day,0,8);return date('Ymd',strtotime('-1 day',strtotime($day)));}
  85. function long_date($ts){return date("Y年n月d日",$ts);}
  86. function day2time($day){return @strtotime($day);}
  87. /*
  88. echo "
    ";
  89. echo strftime("a%an"); // a 星期二
  90. echo strftime("A%An"); // A 星期二
  91. echo strftime("b%bn"); // b 四月
  92. echo strftime("B%Bn"); // B 四月
  93. echo strftime("c%cn"); // c 2006-4-18 3:48:11
  94. echo strftime("C%Cn"); // C
  95. echo strftime("d%dn"); // d 18
  96. echo strftime("D%Dn"); // D
  97. echo strftime("e%en"); // e
  98. echo strftime("g%gn"); // g
  99. echo strftime("G%Gn"); // G
  100. echo strftime("h%hn"); // h
  101. echo strftime("H%Hn"); // H 03
  102. echo strftime("I%In"); // I 03
  103. echo strftime("j%jn"); // j 108
  104. echo strftime("m%mn"); // m 04
  105. echo strftime("M%Mn"); // M 48
  106. echo strftime("n%nn"); // n
  107. echo strftime("p%pn"); // p 上午
  108. echo strftime("r%rn"); // r
  109. echo strftime("R%Rn"); // R
  110. echo strftime("S%Sn"); // S 11
  111. echo strftime("t%tn"); // t
  112. echo strftime("T%Tn"); // T
  113. echo strftime("u%un"); // u
  114. echo strftime("U%Un"); // U 16
  115. echo strftime("V%Vn"); // V
  116. echo strftime("W%Wn"); // W 16
  117. echo strftime("w%wn"); // w 2
  118. echo strftime("x%xn"); // x 2006-4-18
  119. echo strftime("X%Xn"); // X 3:48:11
  120. echo strftime("y%yn"); // y 06
  121. echo strftime("Y%Yn"); // Y 2006
  122. echo strftime("Z%Zn"); // Z 中国标准时间
  123. echo strftime("%%%n"); // %
  124. */
复制代码
  1. function smarty_function_rdate($params, &$smarty){
  2. if (!isset($params['T'])){
  3. $smarty->trigger_error("rdate: missing 'T' parameter");
  4. return;
  5. }
  6. $date = $params['T'];
  7. if (!preg_match('/^d+$/', $date)) $date = strtotime(trim($date));
  8. $sec = time() - $date;
  9. switch(true){
  10. case $sec < 3600:
  11. return round($sec/60). ' 分钟前';
  12. case $sec < 86400:
  13. return round($sec/3600). ' 小时前';
  14. case $sec < (86400 * 7):
  15. return round($sec/86400). ' 天前';//days ago
  16. case $sec < (86400 * 7 * 4):
  17. return round($sec/(86400*7)). ' 周前'; //weeks ago
  18. default:
  19. return date("Y年n月d日",$date);
  20. }
  21. }
  22. ?>
复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP relative path problemNext article:PHP relative path problem