欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP实现计算两个时间戳之间的时间长度

程序员文章站 2022-05-02 09:41:43
...
  1. /**
  2. * 返回两个时间的相距时间,*年*月*日*时*分*秒
  3. * @param int $one_time 时间一
  4. * @param int $two_time 时间二
  5. * @param int $return_type 默认值为0,0/不为0则拼接返回,1/*秒,2/*分*秒,3/*时*分*秒/,4/*日*时*分*秒,5/*月*日*时*分*秒,6/*年*月*日*时*分*秒
  6. * @param array $format_array 格式化字符,例,array('年', '月', '日', '时', '分', '秒')
  7. * @return String or false
  8. */
  9. public function getRemainderTime($one_time, $two_time, $return_type=0, $format_array=array('年', '月', '日', '时', '分', '秒')){
  10. if($return_type6){
  11. return false;
  12. }
  13. if(!(is_int($one_time) && is_int($two_time))){
  14. return false;
  15. }
  16. $remainder_seconds = abs($one_time-$two_time);
  17. //年
  18. $years = 0;
  19. if(($return_type==0 || $return_type==6) && $remainder_seconds-31536000>0){
  20. $years = floor($remainder_seconds/(31536000));
  21. }
  22. //月
  23. $monthes = 0;
  24. if(($return_type==0 || $return_type>=5) && $remainder_seconds-$years*31536000-2592000>0){
  25. $monthes = floor(($remainder_seconds-$years*31536000)/(2592000));
  26. }
  27. //日
  28. $days = 0;
  29. if(($return_type==0 || $return_type>=4) && $remainder_seconds-$years*31536000-$monthes*2592000-86400>0){
  30. $days = floor(($remainder_seconds-$years*31536000-$monthes*2592000)/(86400));
  31. }
  32. //时
  33. $hours = 0;
  34. if(($return_type==0 || $return_type>=3) && $remainder_seconds-$years*31536000-$monthes*2592000-$days*86400-3600>0){
  35. $hours = floor(($remainder_seconds-$years*31536000-$monthes*2592000-$days*86400)/3600);
  36. }
  37. //分
  38. $minutes = 0;
  39. if(($return_type==0 || $return_type>=2) && $remainder_seconds-$years*31536000-$monthes*2592000-$days*86400-$hours*3600-60>0){
  40. $minutes = floor(($remainder_seconds-$years*31536000-$monthes*2592000-$days*86400-$hours*3600)/60);
  41. }
  42. //秒
  43. $seconds = $remainder_seconds-$years*31536000-$monthes*2592000-$days*86400-$hours*3600-$minutes*60;
  44. $return = false;
  45. switch ($return_type){
  46. case 0:
  47. if($years>0){
  48. $return = $years.$format_array[0].$monthes.$format_array[1].$days.$format_array[2].$hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];
  49. }else if($monthes>0){
  50. $return = $monthes.$format_array[1].$days.$format_array[2].$hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];
  51. }else if($days>0){
  52. $return = $days.$format_array[2].$hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];
  53. }else if($hours>0){
  54. $return = $hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];
  55. }else if($minutes>0){
  56. $return = $minutes.$format_array[4].$seconds.$format_array[5];
  57. }else{
  58. $return = $seconds.$format_array[5];
  59. }
  60. break;
  61. case 1:
  62. $return = $seconds.$format_array[5];
  63. break;
  64. case 2:
  65. $return = $minutes.$format_array[4].$seconds.$format_array[5];
  66. break;
  67. case 3:
  68. $return = $hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];
  69. break;
  70. case 4:
  71. $return = $days.$format_array[2].$hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];
  72. break;
  73. case 5:
  74. $return = $monthes.$format_array[1].$days.$format_array[2].$hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];
  75. break;
  76. case 6:
  77. $return = $years.$format_array[0].$monthes.$format_array[1].$days.$format_array[2].$hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];
  78. break;
  79. default:
  80. $return = false;
  81. }
  82. return $return;
  83. }
复制代码

PHP