给定一个月份,要求返回这个月有多少天。

方式有多种,第一种使用内置函数
cal_days_in_month:返回某个历法中某年中某月的天数(推荐学习:PHP编程从入门到精通)
int cal_days_in_month ( int $calendar , int $month , int $year )
calendar:用来计算的历法
立即学习“PHP免费学习笔记(深入)”;
month:选定历法中的某月
year:选定历法中的某年
cal_day_in_month函数需要通过 --enable-calendar 编译 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天';
}
elseif( $month == 2 )
{
if ( $year%400 == 0 || ($year%4 == 0 && $year%100 !== 0) ) //判断是否是闰年
{
$text = $year.'年的'.$month.'月有29天';
}
else{
$text = $year.'年的'.$month.'月有28天';
}
}
else{
$text = $year.'年的'.$month.'月有30天';
}
return $text;
}
$i=2;
$y=2013;
echo get_day($y.'/'.$i);以上就是php获取一个月有几天的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号