php method to calculate the number of weeks in a month: 1. Create a PHP sample file; 2. Create an empty array; 3. Calculate one through the "function get_weekinfo($month){...}" method The number of weeks in a month is sufficient.
The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.
How to calculate the number of weeks in a month in php?
php Calculate the number of weeks in a certain month of a certain year
The code is as follows:
<?php function get_weekinfo($month){ $weekinfo = array();//创建一个空数组 $end_date = date('d',strtotime($month.' +1 month -1 day'));//计算当前月有多少天 for ($i=1; $i <$end_date ; $i=$i+7) { //循环本月有多少周 $w = date('N',strtotime($month.'-'.$i)); //计算第一天是周几 $weekinfo[] = array(date('Y-m-d',strtotime($month.'-'.$i.' -'.($w-1).' days')),date('Y-m-d',strtotime($month.'-'.$i.' +'.(7-$w).' days'))); } //当周开始时间 //结束时间 return $weekinfo; } print_r(get_weekinfo('2017-5'));
Execution result
Array ( [0] => Array ( [0] => 2017-05-01 [1] => 2017-05-07 ) [1] => Array ( [0] => 2017-05-08 [1] => 2017-05-14 ) [2] => Array ( [0] => 2017-05-15 [1] => 2017-05-21 ) [3] => Array ( [0] => 2017-05-22 [1] => 2017-05-28 ) [4] => Array ( [0] => 2017-05-29 [1] => 2017-06-04 ) ) ?>
Recommended learning: "PHP video tutorial"
The above is the detailed content of How to calculate the number of weeks in a month in php. For more information, please follow other related articles on the PHP Chinese website!