php计算几分钟前、几小时前、几天前的几个函数、类分享

╰+哭是因爲堅強的太久メ 2022-08-14 00:44 287阅读 0赞

这篇文章主要介绍了php计算时间几分钟前、几小时前、几天前的几个函数、类分享,需要的朋友可以参考下

一、函数实现

实例1:

  1. function time_tran($the_time){
  2. $now_time = date("Y-m-d H:i:s",time()+8*60*60);
  3. $now_time = strtotime($now_time);
  4. $show_time = strtotime($the_time);
  5. $dur = $now_time - $show_time;
  6. if($dur < 0){
  7. return $the_time;
  8. }else{
  9. if($dur < 60){
  10. return $dur.'秒前';
  11. }else{
  12. if($dur < 3600){
  13. return floor($dur/60).'分钟前';
  14. }else{
  15. if($dur < 86400){
  16. return floor($dur/3600).'小时前';
  17. }else{
  18. if($dur < 259200){//3天内
  19. return floor($dur/86400).'天前';
  20. }else{
  21. return $the_time;
  22. }
  23. }
  24. }

实例2:

  1. <?php
  2. function format_date($time){
  3. $t=time()-$time;
  4. $f=array(
  5. '31536000'=>'年',
  6. '2592000'=>'个月',
  7. '604800'=>'星期',
  8. '86400'=>'天',
  9. '3600'=>'小时',
  10. '60'=>'分钟',
  11. '1'=>'秒'
  12. );
  13. foreach ($f as $k=>$v) {
  14. if (0 !=$c=floor($t/(int)$k)) {
  15. return $c.$v.'前';
  16. }
  17. }
  18. }
  19. ?>

实例3:

  1. function formatTime($date) {
  2. $str = '';
  3. $timer = strtotime($date);
  4. $diff = $_SERVER['REQUEST_TIME'] - $timer;
  5. $day = floor($diff / 86400);
  6. $free = $diff % 86400;
  7. if($day > 0) {
  8. return $day."天前";
  9. }else{
  10. if($free>0){
  11. $hour = floor($free / 3600);
  12. $free = $free % 3600;
  13. if($hour>0){
  14. return $hour."小时前";
  15. }else{
  16. if($free>0){
  17. $min = floor($free / 60);
  18. $free = $free % 60;
  19. if($min>0){
  20. return $min."分钟前";
  21. }else{
  22. if($free>0){
  23. return $free."秒前";
  24. }else{
  25. return '刚刚';
  26. }
  27. }
  28. }else{
  29. return '刚刚';
  30. }
  31. }
  32. }else{
  33. return '刚刚';
  34. }
  35. }
  36. }

实例4:

  1. function time_tran($the_time){
  2. $now_time = date("Y-m-d H:i:s",time()+8*60*60);
  3. $now_time = strtotime($now_time);
  4. $show_time = strtotime($the_time);
  5. $dur = $now_time - $show_time;
  6. if($dur < 0){
  7. return $the_time;
  8. }else{
  9. if($dur < 60){
  10. return $dur.'秒前';
  11. }else{
  12. if($dur < 3600){
  13. return floor($dur/60).'分钟前';
  14. }else{
  15. if($dur < 86400){
  16. return floor($dur/3600).'小时前';
  17. }else{
  18. if($dur < 259200){//3天内
  19. return floor($dur/86400).'天前';
  20. }else{
  21. return $the_time;
  22. }
  23. }
  24. }
  25. }
  26. }
  27. }

二、类的实现

  1. <?php
  2. /*
  3. * author: Solon Ring
  4. * time: 2011-11-02
  5. * 发博时间计算(年,月,日,时,分,秒)
  6. * $createtime 可以是当前时间
  7. * $gettime 你要传进来的时间
  8. */
  9. class Mygettime{
  10. function __construct($createtime,$gettime) {
  11. $this->createtime = $createtime;
  12. $this->gettime = $gettime;
  13. }
  14. function getSeconds()
  15. {
  16. return $this->createtime-$this->gettime;
  17. }
  18. function getMinutes()
  19. {
  20. return ($this->createtime-$this->gettime)/(60);
  21. }
  22. function getHours()
  23. {
  24. return ($this->createtime-$this->gettime)/(60*60);
  25. }
  26. function getDay()
  27. {
  28. return ($this->createtime-$this->gettime)/(60*60*24);
  29. }
  30. function getMonth()
  31. {
  32. return ($this->createtime-$this->gettime)/(60*60*24*30);
  33. }
  34. function getYear()
  35. {
  36. return ($this->createtime-$this->gettime)/(60*60*24*30*12);
  37. }
  38. function index()
  39. {
  40. if($this->getYear() > 1)
  41. {
  42. if($this->getYear() > 2)
  43. {
  44. return date("Y-m-d",$this->gettime);
  45. exit();
  46. }
  47. return intval($this->getYear())." 年前";
  48. exit();
  49. }
  50. if($this->getMonth() > 1)
  51. {
  52. return intval($this->getMonth())." 月前";
  53. exit();
  54. }
  55. if($this->getDay() > 1)
  56. {
  57. return intval($this->getDay())." 天前";
  58. exit();
  59. }
  60. if($this->getHours() > 1)
  61. {
  62. return intval($this->getHours())." 小时前";
  63. exit();
  64. }
  65. if($this->getMinutes() > 1)
  66. {
  67. return intval($this->getMinutes())." 分钟前";
  68. exit();
  69. }
  70. if($this->getSeconds() > 1)
  71. {
  72. return intval($this->getSeconds()-1)." 秒前";
  73. exit();
  74. }
  75. }
  76. }
  77. //类的使用实例
  78. /*
  79. *
  80. * 调用类输出方式
  81. *
  82. * $a = new Mygettime(time(),strtotime('-25 month'));
  83. * echo iconv('utf-8', 'gb2312', $a->index())?iconv('utf-8', 'gb2312', $a->index()):iconv('utf-8', 'gb2312', '当前');
  84. *
  85. */

发表评论

表情:
评论列表 (有 0 条评论,287人围观)

还没有评论,来说两句吧...

相关阅读