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 相关文章推荐
刚才在简化php的库,结果发现很多东西
Dec 31 PHP
创建数据库php代码 用PHP写出自己的BLOG系统
Apr 12 PHP
PHP面向对象分析设计的61条军规小结
Jul 17 PHP
PHP日期处理函数 整型日期格式
Jan 12 PHP
php 操作符与控制结构
Mar 07 PHP
php mysql_real_escape_string函数用法与实例教程
Sep 30 PHP
async和DOM Script文件加载比较
Jul 20 PHP
php利用cookie实现自动登录的方法
Dec 10 PHP
php实现概率性随机抽奖代码
Jan 02 PHP
PHP指定截取字符串中的中英文或数字字符的实例分享
Mar 18 PHP
ThinkPHP5&amp;5.1框架关联模型分页操作示例
Aug 03 PHP
thinkphp5.1框架实现格式化mysql时间戳为日期的方式小结
Oct 10 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
利用phpExcel实现Excel数据的导入导出(全步骤详细解析)
2013/11/26 PHP
PHP错误和异长常处理总结
2014/03/06 PHP
PJBlog插件 防刷新的在线播放器
2006/10/25 Javascript
禁止刷新,回退的JS
2006/11/25 Javascript
js实现的网页颜色代码表全集
2007/07/17 Javascript
js和jquery中循环的退出和继续学习记录
2014/09/06 Javascript
jQuery 回调函数(callback)的使用和基础
2015/02/26 Javascript
js闭包实现按秒计数
2015/04/23 Javascript
javascript适合移动端的日期时间拾取器
2015/11/10 Javascript
ajax在兼容模式下失效的快速解决方法
2016/03/22 Javascript
jQuery实现导航高亮的方法【附demo源码下载】
2016/11/09 Javascript
jQuery设置和获取select、checkbox、radio的选中值方法
2017/01/01 Javascript
浅析Vue项目中使用keep-Alive步骤
2018/07/27 Javascript
ES6 系列之 Generator 的自动执行的方法示例
2018/10/19 Javascript
用webpack4开发小程序的实现方法
2019/06/04 Javascript
微信用户访问小程序的登录过程详解
2019/09/20 Javascript
分享JS表单验证源码(带错误提示及密码等级)
2020/01/05 Javascript
EM算法的python实现的方法步骤
2018/01/02 Python
解决pandas .to_excel不覆盖已有sheet的问题
2018/12/10 Python
在python中将字符串转为json对象并取值的方法
2018/12/31 Python
python 定时器,轮询定时器的实例
2019/02/20 Python
Django如何防止定时任务并发浅析
2019/05/14 Python
python实现的接收邮件功能示例【基于网易POP3服务器】
2019/09/11 Python
Python concurrent.futures模块使用实例
2019/12/24 Python
解决Jupyter Notebook开始菜单栏Anaconda下消失的问题
2020/04/13 Python
使用简单的CSS3属性实现炫酷读者墙效果
2014/01/08 HTML / CSS
以特惠价提供在线奢侈品购物:FRMODA.com
2018/01/25 全球购物
能源工程专业应届生求职信
2014/03/01 职场文书
安全宣传标语口号
2014/06/06 职场文书
国庆节促销广告语2014
2014/09/19 职场文书
公司员工离职证明书
2014/10/04 职场文书
2014年药品销售工作总结
2014/12/16 职场文书
大二学年个人总结
2015/03/03 职场文书
清洁工个人工作总结
2015/03/05 职场文书
爱国主义教育主题班会
2015/08/13 职场文书
辞职申请书范本
2019/05/20 职场文书