How to calculate the number of weeks in a month in php

藏色散人
Release: 2023-03-12 21:44:01
Original
2397 people have browsed it

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.

How to calculate the number of weeks in a month in php

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(&#39;d&#39;,strtotime($month.&#39; +1 month -1 day&#39;));//计算当前月有多少天
    for ($i=1; $i <$end_date ; $i=$i+7) {   //循环本月有多少周
        $w = date(&#39;N&#39;,strtotime($month.&#39;-&#39;.$i));  //计算第一天是周几
        $weekinfo[] = array(date(&#39;Y-m-d&#39;,strtotime($month.&#39;-&#39;.$i.&#39; -&#39;.($w-1).&#39; days&#39;)),date(&#39;Y-m-d&#39;,strtotime($month.&#39;-&#39;.$i.&#39; +&#39;.(7-$w).&#39; days&#39;)));
    }                                                    //当周开始时间                    //结束时间
    return $weekinfo;
}
print_r(get_weekinfo(&#39;2017-5&#39;));
Copy after login

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
        )
)
?>
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!