分享3个php获取日历的函数

本文给大家汇总了3个php获取日历的函数,分别是php获取指定日期的月份的日历、获取指定日期所在月的开始日期与结束日期、获取当前星期的日期范围,都是比较常用的方法,有需要的小伙伴可以参考下。

提供一个日期,获取这个日期的星期对应日历列表,键为星期标示:

  1. $month_date = '2015-09-25';
  2. $start_time = strtotime($month_date);
  3. $start_week = date('w', $start_time);
  4. $total_month_day = date('t', $start_time);
  5. $weeks_in_month = ceil(($start_week+$total_month_day)/7);
  6. $month_day_arr = [];
  7. $start_month_day = 1;
  8. for($i=0;$i<$weeks_in_month;$i++) {
  9. for($j=0;$j<7;$j++){
  10. if($i ==0 && $j >= $start_week) {
  11. $month_day_arr[$i][$j] = $start_month_day;
  12. $start_month_day++;
  13. } elseif($i == 0) {
  14. $month_day_arr[$i][$j] = '';
  15. } else {
  16. $month_day_arr[$i][$j] = $start_month_day;
  17. $start_month_day++;
  18. }
  19. if($start_month_day > $total_month_day){
  20. break;
  21. }
  22. }
  23. }
  24. echo "<pre>";
  25. print_r($month_day_arr);
  26. Array
  27. (
  28. [0] => Array
  29. (
  30. [0] =>
  31. [1] =>
  32. [2] => 1
  33. [3] => 2
  34. [4] => 3
  35. [5] => 4
  36. [6] => 5
  37. )
  38. [1] => Array
  39. (
  40. [0] => 6
  41. [1] => 7
  42. [2] => 8
  43. [3] => 9
  44. [4] => 10
  45. [5] => 11
  46. [6] => 12
  47. )
  48. [2] => Array
  49. (
  50. [0] => 13
  51. [1] => 14
  52. [2] => 15
  53. [3] => 16
  54. [4] => 17
  55. [5] => 18
  56. [6] => 19
  57. )
  58. [3] => Array
  59. (
  60. [0] => 20
  61. [1] => 21
  62. [2] => 22
  63. [3] => 23
  64. [4] => 24
  65. [5] => 25
  66. [6] => 26
  67. )
  68. [4] => Array
  69. (
  70. [0] => 27
  71. [1] => 28
  72. [2] => 29
  73. [3] => 30
  74. )
  75. )

获取指定日期所在月的开始日期与结束日期:

  1. /**
  2. * @param string $date
  3. * @param boolean 为true返回开始日期,否则返回结束日期
  4. * @return array
  5. * @access private
  6. */
  7. private function getMonthRange( $date, $returnFirstDay = true ) {
  8. $timestamp = strtotime( $date );
  9. if ( $returnFirstDay ) {
  10. $monthFirstDay = date( 'Y-m-1 00:00:00', $timestamp );
  11. return $monthFirstDay;
  12. } else {
  13. $mdays = date( 't', $timestamp );
  14. $monthLastDay = date( 'Y-m-' . $mdays . ' 23:59:59', $timestamp );
  15. return $monthLastDay;
  16. }
  17. }

获取当前星期的日期范围,也就是从星期一到星期日的日期范围。

  1. function getWeekRange($date){
  2. $ret=array();
  3. $timestamp=strtotime($date);
  4. $w=strftime('%u',$timestamp);
  5. $ret['sdate']=date('Y-m-d 00:00:00',$timestamp-($w-1)*86400);
  6. $ret['edate']=date('Y-m-d 23:59:59',$timestamp+(7-$w)*86400);
  7. return $ret;
  8. }
  9. //author:zhxia 获取指定日期所在月的开始日期与结束日期
  10. function getMonthRange($date){
  11. $ret=array();
  12. $timestamp=strtotime($date);
  13. $mdays=date('t',$timestamp);
  14. $ret['sdate']=date('Y-m-1 00:00:00',$timestamp);
  15. $ret['edate']=date('Y-m-'.$mdays.' 23:59:59',$timestamp);
  16. return $ret;
  17. }
  18. //author:zhxia 以上两个函数的应用
  19. function getFilter($n){
  20. $ret=array();
  21. switch($n){
  22. case 1:// 昨天
  23. $ret['sdate']=date('Y-m-d 00:00:00',strtotime('-1 day'));
  24. $ret['edate']=date('Y-m-d 23:59:59',strtotime('-1 day'));
  25. break;
  26. case 2://本星期
  27. $ret=getWeekRange(date('Y-m-d'));
  28. break;
  29. case 3://上一个星期
  30. $strDate=date('Y-m-d',strtotime('-1 week'));
  31. $ret=getWeekRange($strDate);
  32. break;
  33. case 4: //上上星期
  34. $strDate=date('Y-m-d',strtotime('-2 week'));
  35. $ret=getWeekRange($strDate);
  36. break;
  37. case 5: //本月
  38. $ret=getMonthRange(date('Y-m-d'));
  39. break;
  40. case 6://上月
  41. $strDate=date('Y-m-d',strtotime('-1 month'));
  42. $ret=getMonthRange($strDate);
  43. break;
  44. }
  45. return $ret;
  46. }