Home > Backend Development > PHP Tutorial > PHP code example to calculate the number of years, months, weeks, and days between two dates

PHP code example to calculate the number of years, months, weeks, and days between two dates

WBOY
Release: 2016-07-25 08:58:51
Original
1424 people have browsed it
  1. /**

  2. * Calculate the number of years, months, weeks and days between two dates
  3. * edit bbs.it-home.org
  4. */
  5. function format($a,$b){
  6. //Check the size of two dates, the default is small in the front and large in the back, if If the front is big and the back is small, the positions are swapped to ensure that the front is small and the back is big
  7. if(strtotime($a)>strtotime($b)) list($a,$b)=array($b,$a);
  8. $start = strtotime($a);
  9. $stop = strtotime($b);
  10. $extend = ($stop-$start)/86400;
  11. $result['extends'] = $extend;
  12. if($extend<7 ){ //If it is less than 7 days, return the number of days directly
  13. $result['daily'] = $extend;
  14. }elseif($extend<=31){ //If it is less than 28 days, return the number of weeks, because February of the leap year meets
  15. if($stop==strtotime($a.'+1 month')){
  16. $result['monthly'] = 1;
  17. }else{
  18. $w = floor($extend/7);
  19. $d = ($stop-strtotime($a.'+'.$w.' week'))/86400;
  20. $result['weekly'] = $w;
  21. $result['daily'] = $d;
  22. }
  23. }else{
  24. $y= floor($extend/365);
  25. if($y>=1){ //If more than one year
  26. $start = strtotime($a.'+'.$y.' year');
  27. $a = date('Y-m-d',$start);
  28. //Determine whether it has really been a year, if not, reduce it
  29. if($start>$stop){
  30. $a = date('Y-m-d',strtotime($a.'-1 month'));
  31. $m =11;
  32. $y--;
  33. }
  34. $extend = ($stop-strtotime($ a))/86400;
  35. }
  36. if(isset($m)){
  37. $w = floor($extend/7);
  38. $d = $extend-$w*7;
  39. }else{
  40. $m = isset($m)?$m:round($extend/30);
  41. $stop>=strtotime($a.'+'.$m.'month')?$m:$m--;
  42. if( $stop>=strtotime($a.'+'.$m.'month')){
  43. $d=$w=($stop-strtotime($a.'+'.$m.'month')) /86400;
  44. $w = floor($w/7);
  45. $d = $d-$w*7;
  46. }
  47. }
  48. $result['yearly'] = $y;
  49. $result['monthly' ] = $m;
  50. $result['weekly'] = $w;
  51. $result['daily'] = isset($d)?$d:null;
  52. }
  53. return array_filter($result);
  54. }< ;/p>
  55. print_r(format('2012-10-1','2012-12-15'));

  56. ?>

Copy code

output result: Array([extends]=>75[monthly]=>2[weekly]=>2)

2. PHP queries the week number of a certain day and the starting date of the corresponding week

  1. /**
  2. * @file
  3. * @version 1.1
  4. */
  5. //Get the week number of a certain date and the corresponding start and end time of the week
  6. private function getWeekStartEndDay($day)
  7. {
  8. $g = strftime("%u",strtotime($day));
  9. return array('week_num'=>strftime("%V",strtotime($day)),'week_start_day'=>strftime('%Y- %m-%d',strtotime($day)-($g-1)*86400),'week_start_day_cn'=>strftime('%Y year %m month %d day',strtotime($day)-( $g-1)*86400),'week_end_day'=>strftime('%Y-%m-%d',strtotime($day) + (7-$g)*86400),'week_end_day_cn'=> strftime('%Y year %m month %d day',strtotime($day) + (7-$g)*86400));
  10. }
  11. ?>
Copy code


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