本文实例讲述了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中文网(www.php.cn)!

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

PHP不是在消亡,而是在不断适应和进化。1)PHP从1994年起经历多次版本迭代,适应新技术趋势。2)目前广泛应用于电子商务、内容管理系统等领域。3)PHP8引入JIT编译器等功能,提升性能和现代化。4)使用OPcache和遵循PSR-12标准可优化性能和代码质量。

PHP的未来将通过适应新技术趋势和引入创新特性来实现:1)适应云计算、容器化和微服务架构,支持Docker和Kubernetes;2)引入JIT编译器和枚举类型,提升性能和数据处理效率;3)持续优化性能和推广最佳实践。

在PHP中,trait适用于需要方法复用但不适合使用继承的情况。1)trait允许在类中复用方法,避免多重继承复杂性。2)使用trait时需注意方法冲突,可通过insteadof和as关键字解决。3)应避免过度使用trait,保持其单一职责,以优化性能和提高代码可维护性。

依赖注入容器(DIC)是一种管理和提供对象依赖关系的工具,用于PHP项目中。DIC的主要好处包括:1.解耦,使组件独立,代码易维护和测试;2.灵活性,易替换或修改依赖关系;3.可测试性,方便注入mock对象进行单元测试。

SplFixedArray在PHP中是一种固定大小的数组,适用于需要高性能和低内存使用量的场景。1)它在创建时需指定大小,避免动态调整带来的开销。2)基于C语言数组,直接操作内存,访问速度快。3)适合大规模数据处理和内存敏感环境,但需谨慎使用,因其大小固定。

PHP通过$\_FILES变量处理文件上传,确保安全性的方法包括:1.检查上传错误,2.验证文件类型和大小,3.防止文件覆盖,4.移动文件到永久存储位置。

JavaScript中处理空值可以使用NullCoalescingOperator(??)和NullCoalescingAssignmentOperator(??=)。1.??返回第一个非null或非undefined的操作数。2.??=将变量赋值为右操作数的值,但前提是该变量为null或undefined。这些操作符简化了代码逻辑,提高了可读性和性能。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

SublimeText3汉化版
中文版,非常好用

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。