当前位置:Gxlcms > PHP教程 > php根据年月获取当月天数及日期数组的方法图文详解

php根据年月获取当月天数及日期数组的方法图文详解

时间:2021-07-01 10:21:17 帮助过:7人阅读

这篇文章主要介绍了php根据年月获取当月天数及日期数组的方法,涉及php针对日期的相关判断、转换及字符与数组的遍历操作相关技巧,需要的朋友可以参考下

具体如下:

  1. function get_day( $date )
  2. {
  3. $tem = explode('-' , $date); //切割日期 得到年份和月份
  4. $year = $tem['0'];
  5. $month = $tem['1'];
  6. if( in_array($month , array( 1 , 3 , 5 , 7 , 8 , 01 , 03 , 05 , 07 , 08 , 10 , 12)))
  7. {
  8. // $text = $year.'年的'.$month.'月有31天';
  9. $text = '31';
  10. }
  11. elseif( $month == 2 )
  12. {
  13. if ( $year%400 == 0 || ($year%4 == 0 && $year%100 !== 0) ) //判断是否是闰年
  14. {
  15. // $text = $year.'年的'.$month.'月有29天';
  16. $text = '29';
  17. }
  18. else{
  19. // $text = $year.'年的'.$month.'月有28天';
  20. $text = '28';
  21. }
  22. }
  23. else{
  24. // $text = $year.'年的'.$month.'月有30天';
  25. $text = '30';
  26. }
  27. return $text;
  28. }
  29. echo get_day('2016-8-1');

运行结果为:31

改造,返回日期数组:

  1. /**
  2. * 获取当月天数
  3. * @param $date
  4. * @param $rtype 1天数 2具体日期数组
  5. * @return
  6. */
  7. function get_day( $date ,$rtype = '1')
  8. {
  9. $tem = explode('-' , $date); //切割日期 得到年份和月份
  10. $year = $tem['0'];
  11. $month = $tem['1'];
  12. if( in_array($month , array( 1 , 3 , 5 , 7 , 8 , 01 , 03 , 05 , 07 , 08 , 10 , 12)))
  13. {
  14. // $text = $year.'年的'.$month.'月有31天';
  15. $text = '31';
  16. }
  17. elseif( $month == 2 )
  18. {
  19. if ( $year%400 == 0 || ($year%4 == 0 && $year%100 !== 0) ) //判断是否是闰年
  20. {
  21. // $text = $year.'年的'.$month.'月有29天';
  22. $text = '29';
  23. }
  24. else{
  25. // $text = $year.'年的'.$month.'月有28天';
  26. $text = '28';
  27. }
  28. }
  29. else{
  30. // $text = $year.'年的'.$month.'月有30天';
  31. $text = '30';
  32. }
  33. if ($rtype == '2') {
  34. for ($i = 1; $i <= $text ; $i ++ ) {
  35. $r[] = $year."-".$month."-".$i;
  36. }
  37. } else {
  38. $r = $text;
  39. }
  40. return $r;
  41. }
  42. var_dump(get_day('2016-8-1','2'));

运行结果如下:

  1. array(31) {
  2. [0]=>
  3. string(8) "2016-8-1"
  4. [1]=>
  5. string(8) "2016-8-2"
  6. [2]=>
  7. string(8) "2016-8-3"
  8. [3]=>
  9. string(8) "2016-8-4"
  10. [4]=>
  11. string(8) "2016-8-5"
  12. [5]=>
  13. string(8) "2016-8-6"
  14. [6]=>
  15. string(8) "2016-8-7"
  16. [7]=>
  17. string(8) "2016-8-8"
  18. [8]=>
  19. string(8) "2016-8-9"
  20. [9]=>
  21. string(9) "2016-8-10"
  22. [10]=>
  23. string(9) "2016-8-11"
  24. [11]=>
  25. string(9) "2016-8-12"
  26. [12]=>
  27. string(9) "2016-8-13"
  28. [13]=>
  29. string(9) "2016-8-14"
  30. [14]=>
  31. string(9) "2016-8-15"
  32. [15]=>
  33. string(9) "2016-8-16"
  34. [16]=>
  35. string(9) "2016-8-17"
  36. [17]=>
  37. string(9) "2016-8-18"
  38. [18]=>
  39. string(9) "2016-8-19"
  40. [19]=>
  41. string(9) "2016-8-20"
  42. [20]=>
  43. string(9) "2016-8-21"
  44. [21]=>
  45. string(9) "2016-8-22"
  46. [22]=>
  47. string(9) "2016-8-23"
  48. [23]=>
  49. string(9) "2016-8-24"
  50. [24]=>
  51. string(9) "2016-8-25"
  52. [25]=>
  53. string(9) "2016-8-26"
  54. [26]=>
  55. string(9) "2016-8-27"
  56. [27]=>
  57. string(9) "2016-8-28"
  58. [28]=>
  59. string(9) "2016-8-29"
  60. [29]=>
  61. string(9) "2016-8-30"
  62. [30]=>
  63. string(9) "2016-8-31"
  64. }

以上就是本文的全部内容,希望对大家的学习有所帮助。


相关推荐:

php面向对象之反射功能与用法

PHP反射机制实例详解

PHP实现针对中英文混合字符串长度判断及截取方法

以上就是php根据年月获取当月天数及日期数组的方法图文详解的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行