[关闭]
@fzbing 2015-06-09T16:41:18.000000Z 字数 1227 阅读 801

php日期范围

php


  1. function timeto($time, $case = 1)
  2. {
  3. $Y = date('Y', $time);
  4. $m = date('m', $time);
  5. $d = date('d', $time);
  6. $z = date('z', $time);
  7. $z++;
  8. switch ($case) {
  9. case 1: // 天
  10. $last_start = mktime(0, 0, 0, 1, $z - 1, $Y);
  11. $cur_start = mktime(0, 0, 0, 1, $z, $Y);
  12. $next_start = mktime(0, 0, 0, 1, $z + 1, $Y);
  13. break;
  14. case 2: // 周
  15. $N = date('N', $time);
  16. $z = $z - $N + 1;
  17. $last_start = mktime(0, 0, 0, 1, $z - 7, $Y);
  18. $cur_start = mktime(0, 0, 0, 1, $z, $Y);
  19. $next_start = mktime(0, 0, 0, 1, $z + 7, $Y);
  20. break;
  21. case 3:
  22. $j = date('j', $time); // 当前月份第几天
  23. $cur_month_days = date('t', $time);
  24. $last_month_days = date('t', mktime(0, 0, 0, $m - 1, $d, $Y));
  25. $z = $z - $j + 1;
  26. $last_start = mktime(0, 0, 0, 1, $z - $last_month_days, $Y);
  27. $cur_start = mktime(0, 0, 0, 1, $z, $Y);
  28. $next_start = mktime(0, 0, 0, 1, $z + $cur_month_days, $Y);
  29. break;
  30. case 4:
  31. $j = date('j', $time); // 当前月份第几天
  32. $cur_months_days = date('t', $time)
  33. + date('t', mktime(0, 0, 0, $m + 1, $d, $Y))
  34. + date('t', mktime(0, 0, 0, $m + 2, $d, $Y));
  35. $last_months_days = date('t', mktime(0, 0, 0, $m - 3, $d, $Y))
  36. + date('t', mktime(0, 0, 0, $m - 2, $d, $Y))
  37. + date('t', mktime(0, 0, 0, $m - 1, $d, $Y));
  38. $z = $z - $j + 1; // 当前月第一天
  39. $last_start = mktime(0, 0, 0, 1, $z - $last_months_days, $Y);
  40. $cur_start = mktime(0, 0, 0, 1, $z, $Y);
  41. $next_start = mktime(0, 0, 0, 1, $z + $cur_months_days, $Y);
  42. break;
  43. default:
  44. return false;
  45. }
  46. return [
  47. 'cur' => [
  48. 'start' => $cur_start,
  49. 'end' => $next_start - 1
  50. ],
  51. 'last' => [
  52. 'start' => $last_start,
  53. 'end' => $cur_start - 1
  54. ],
  55. ];
  56. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注