Home >Backend Development >PHP Tutorial >In php, get the number of days of the month for a specified date
At work, I often encounter the problem of getting the number of days in a certain month. This is how I solved it before.
<code><span>//这里以2015年11月为例</span><span>$inputs</span>[<span>'month'</span>] = <span>'201511'</span>;<span>//传递过来的参数</span><span>$tmp_month</span> = <span>$inputs</span>[<span>'month'</span>].<span>"01"</span>; <span>//$tmp_month = '20151101';</span><span>$s_time</span> = strtotime(<span>$tmp_month</span>);<span>//月起始时间</span><span>$e_time</span> = strtotime(<span>date</span>(<span>"Ymd"</span>,<span>$s_time</span>).<span>'+1 month'</span>);<span>//月结束时间</span><span>$countMins</span> = (<span>$e_time</span> - <span>$s_time</span>)/<span>60</span>;<span>//当月分钟数</span><span>$countDay</span> = <span>$countMins</span>/(<span>60</span><span>*24</span>);<span>//当月的天数</span></code>
<code>但是现在,发现了更好的获取方法。 </code>
php has built-in function date(), which can directly get the number of days in the month.
<code><span>$tmp_month</span> = <span>$inputs</span>[<span>'month'</span>].<span>"01"</span>; <span>$countDay</span> = date(<span>"t"</span>,strtotime(<span>$tmp_month</span>)); <span>//</span>如果直接date(<span>"t"</span>),得到的是当月的天数</code>
so easy!
<code>其实,还有一个方法,使用cal_days_in_month()函数,php手册中给的解释是:返回某个历法中某年中某月的天数。 </code>
3 parameters:
calendar A certain calendar used for calculations
month selects a month in the calendar
year selects a certain year in the calendar
<code><span>$num</span> = cal_days_<span>in</span>_month(CAL_GREGORIAN, <span>11</span>, <span>2015</span>); // <span>30</span></code>
Personally, I prefer the date() function, so that I don’t have to consider calendar issues.
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });The above introduces how to get the number of days of the month on a specified date in PHP, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.