Home > Backend Development > PHP Tutorial > PHP implementation to calculate the length of time between two timestamps

PHP implementation to calculate the length of time between two timestamps

WBOY
Release: 2016-07-25 08:45:23
Original
1453 people have browsed it
  1. /**
  2. * Return the time between two times, *year*month*day*hour*minute*second
  3. * @param int $one_time time one
  4. * @param int $two_time time two
  5. * @param int $return_type The default value is 0, if 0/ is not 0, the concatenation returns, 1/* second, 2/* minute* second, 3/* hour* minute* second/, 4/* day* hour* minute* second, 5/* month* day *hour*minute*second, 6/*year*month*day*hour*minute*second
  6. * @param array $format_array formatting characters, for example, array('year', 'month', 'day', 'hour ', 'minutes', 'seconds')
  7. * @return String or false
  8. */
  9. public function getRemainderTime($one_time, $two_time, $return_type=0, $format_array=array('年', '月', '日', '时', '分', '秒')){
  10. if($return_type<0 || $return_type>6){
  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


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template