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


Posted in PHP onNovember 30, 2016

本文实例讲述了php根据年月获取当月天数及日期数组的方法。分享给大家供大家参考,具体如下:

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

运行结果为:31

改造,返回日期数组:

/**
* 获取当月天数
* @param $date 
* @param $rtype 1天数 2具体日期数组
* @return 
*/
function get_day( $date ,$rtype = '1')  
{
    $tem = explode('-' , $date);    //切割日期 得到年份和月份
    $year = $tem['0'];
    $month = $tem['1'];
    if( in_array($month , array( 1 , 3 , 5 , 7 , 8 , 01 , 03 , 05 , 07 , 08 , 10 , 12)))
    {
      // $text = $year.'年的'.$month.'月有31天';
      $text = '31';
    }
    elseif( $month == 2 )
    {
      if ( $year%400 == 0 || ($year%4 == 0 && $year%100 !== 0) )    //判断是否是闰年
      {
        // $text = $year.'年的'.$month.'月有29天';
        $text = '29';
      }
      else{
        // $text = $year.'年的'.$month.'月有28天';
        $text = '28';
      }
    }
    else{
      // $text = $year.'年的'.$month.'月有30天';
      $text = '30';
    }
    if ($rtype == '2') {
      for ($i = 1; $i <= $text ; $i ++ ) {
        $r[] = $year."-".$month."-".$i;
      }
    } else {
      $r = $text;
    }
    return $r;
}
var_dump(get_day('2016-8-1','2'));

运行结果如下:

array(31) {
 [0]=>
 string(8) "2016-8-1"
 [1]=>
 string(8) "2016-8-2"
 [2]=>
 string(8) "2016-8-3"
 [3]=>
 string(8) "2016-8-4"
 [4]=>
 string(8) "2016-8-5"
 [5]=>
 string(8) "2016-8-6"
 [6]=>
 string(8) "2016-8-7"
 [7]=>
 string(8) "2016-8-8"
 [8]=>
 string(8) "2016-8-9"
 [9]=>
 string(9) "2016-8-10"
 [10]=>
 string(9) "2016-8-11"
 [11]=>
 string(9) "2016-8-12"
 [12]=>
 string(9) "2016-8-13"
 [13]=>
 string(9) "2016-8-14"
 [14]=>
 string(9) "2016-8-15"
 [15]=>
 string(9) "2016-8-16"
 [16]=>
 string(9) "2016-8-17"
 [17]=>
 string(9) "2016-8-18"
 [18]=>
 string(9) "2016-8-19"
 [19]=>
 string(9) "2016-8-20"
 [20]=>
 string(9) "2016-8-21"
 [21]=>
 string(9) "2016-8-22"
 [22]=>
 string(9) "2016-8-23"
 [23]=>
 string(9) "2016-8-24"
 [24]=>
 string(9) "2016-8-25"
 [25]=>
 string(9) "2016-8-26"
 [26]=>
 string(9) "2016-8-27"
 [27]=>
 string(9) "2016-8-28"
 [28]=>
 string(9) "2016-8-29"
 [29]=>
 string(9) "2016-8-30"
 [30]=>
 string(9) "2016-8-31"
}

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
基于mysql的论坛(5)
Oct 09 PHP
如何使用PHP中的字符串函数
Nov 24 PHP
解析PHP实现多进程并行执行脚本
Jun 18 PHP
php的declare控制符和ticks教程(附示例)
Mar 21 PHP
php支付宝接口用法分析
Jan 04 PHP
详解Window7 下开发php扩展
Dec 31 PHP
Docker 如何布置PHP开发环境
Jun 21 PHP
Yii列表定义与使用分页方法小结(3种方法)
Jul 15 PHP
PHP 7.0新增加的特性介绍
Jun 08 PHP
PHP数组内存利用率低和弱类型详细解读
Aug 10 PHP
让Laravel API永远返回JSON格式响应的方法示例
Sep 05 PHP
php策略模式简单示例分析【区别于工厂模式】
Sep 25 PHP
详解PHP处理密码的几种方式
Nov 30 #PHP
php+js实现百度地图多点标注的方法
Nov 30 #PHP
php 运算符与表达式详细介绍
Nov 30 #PHP
PHP AjaxForm提交图片上传并显示图片源码
Nov 29 #PHP
php判断是否为ajax请求的方法
Nov 29 #PHP
PHP判断文件是否被引入的方法get_included_files用法示例
Nov 29 #PHP
php获取开始与结束日期之间所有日期的方法
Nov 29 #PHP
You might like
php模拟socket一次连接,多次发送数据的实现代码
2011/07/26 PHP
php数组函数序列 之array_count_values() 统计数组中所有值出现的次数函数
2011/10/29 PHP
PHP类的静态(static)方法和静态(static)变量使用介绍
2012/02/19 PHP
php相对当前文件include其它文件的方法
2015/03/13 PHP
jQuery News Ticker 基于jQuery的即时新闻行情展示插件
2011/11/05 Javascript
JS字符串拼接在ie中都报错的解决方法
2014/03/27 Javascript
如何让浏览器支持jquery ajax load 前进、后退功能
2014/06/12 Javascript
javascript引用赋值(地址传值)用法实例
2015/01/13 Javascript
JavaScript中创建字典对象(dictionary)实例
2015/03/31 Javascript
解决jQuery使用JSONP时产生的错误
2015/12/02 Javascript
js文本框输入内容智能提示效果
2015/12/02 Javascript
jQuery实例—选项卡的简单实现(js源码和jQuery)
2016/06/14 Javascript
基于复选框demo(分享)
2017/09/27 Javascript
聊聊Vue.js的template编译的问题
2017/10/09 Javascript
详解webpack babel的配置
2018/01/09 Javascript
Vue中在新窗口打开页面及Vue-router的使用
2018/06/13 Javascript
Vue基于vuex、axios拦截器实现loading效果及axios的安装配置
2019/04/26 Javascript
纯异步nodejs文件夹(目录)复制功能
2019/09/03 NodeJs
jQuery实现简易QQ聊天框
2020/02/10 jQuery
VUE页面中通过双击实现复制表格中内容的示例代码
2020/06/11 Javascript
[05:24]TI9采访——教练
2019/08/24 DOTA
Python学生信息管理系统修改版
2018/03/13 Python
selenium+python实现1688网站验证码图片的截取功能
2018/08/14 Python
Numpy截取指定范围内的数据方法
2018/11/14 Python
selenium3+python3环境搭建教程图解
2018/12/07 Python
python3实现网络爬虫之BeautifulSoup使用详解
2018/12/19 Python
django 中的聚合函数,分组函数,F 查询,Q查询
2019/07/25 Python
BONIA官方网站:国际奢侈品牌和皮革专家
2016/11/27 全球购物
奥地利领先的在线药房:SHOP APOTHEKE
2019/10/07 全球购物
文秘档案管理岗位职责
2014/03/06 职场文书
奥巴马获胜演讲稿
2014/05/15 职场文书
物理系毕业生自荐书
2014/06/13 职场文书
经济国贸专业求职信
2014/06/18 职场文书
自考生自我评价
2019/06/21 职场文书
90条交通安全宣传标语
2019/10/12 职场文书
Windows和Linux上部署Golang并运行程序
2022/04/22 Servers