How to calculate the difference in weeks between two dates in php

藏色散人
Release: 2023-03-11 20:00:02
Original
1851 people have browsed it

php method to calculate the weeks difference between two dates: first create a PHP sample file; then calculate the interval between two dates through methods such as "function format($a,$b){...}" of weeks is enough.

How to calculate the difference in weeks between two dates in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How does php calculate the difference between two dates? week?

Use PHP to calculate the number of years, months, weeks, and days between two dates:

The code is as follows:

<?php
    function format($a,$b){
        //检查两个日期大小,默认前小后大,如果前大后小则交换位置以保证前小后大
        if(strtotime($a)>strtotime($b)) list($a,$b)=array($b,$a);
        $start  = strtotime($a);
        $stop   = strtotime($b);
        $extend = ($stop-$start)/86400;
        $result[&#39;extends&#39;] = $extend;
        if($extend<7){                //如果小于7天直接返回天数
            $result[&#39;daily&#39;] = $extend;
        }elseif($extend<=31){        //小于28天则返回周数,由于闰年2月满足了
            if($stop==strtotime($a.&#39;+1 month&#39;)){
                $result[&#39;monthly&#39;] = 1;
            }else{
                $w = floor($extend/7);
                $d = ($stop-strtotime($a.&#39;+&#39;.$w.&#39; week&#39;))/86400;
                $result[&#39;weekly&#39;]  = $w;
                $result[&#39;daily&#39;]   = $d;
            }
        }else{
            $y=    floor($extend/365);
            if($y>=1){                //如果超过一年
                $start = strtotime($a.&#39;+&#39;.$y.&#39;year&#39;);
                $a     = date(&#39;Y-m-d&#39;,$start);
                //判断是否真的已经有了一年了,如果没有的话就开减
                if($start>$stop){
                    $a = date(&#39;Y-m-d&#39;,strtotime($a.&#39;-1 month&#39;));
                    $m =11;
                    $y--;   
                }
                $extend = ($stop-strtotime($a))/86400;
            }
            if(isset($m)){
                $w = floor($extend/7);
                $d = $extend-$w*7;
            }else{
                $m = isset($m)?$m:round($extend/30);
                $stop>=strtotime($a.&#39;+&#39;.$m.&#39;month&#39;)?$m:$m--;
                if($stop>=strtotime($a.&#39;+&#39;.$m.&#39;month&#39;)){
                    $d=$w=($stop-strtotime($a.&#39;+&#39;.$m.&#39;month&#39;))/86400;
                    $w = floor($w/7);
                    $d = $d-$w*7;
                }
            }
            $result[&#39;yearly&#39;]  = $y;
            $result[&#39;monthly&#39;] = $m;
            $result[&#39;weekly&#39;]  = $w;
            $result[&#39;daily&#39;]   = isset($d)?$d:null;
        }
        return array_filter($result);
    }
    print_r(format(&#39;2012-10-1&#39;,&#39;2012-12-15&#39;));
?>
Copy after login

Running results:

Array([extends]=>75[monthly]=>2[weekly]=>2)
Copy after login

php Query the week number of a certain day and the starting date of the corresponding week

The code is as follows:

/**
* @file
* @version  1.1
* @author  QQ83989686
* @date  2012-8-7 最后修改时间
* @brief
*/
    //获取某个日期的 周数、周对应的开始结束时间
    private function getWeekStartEndDay($day)
    {
        $g  = strftime("%u",strtotime($day));
        return array(&#39;week_num&#39;=>strftime("%V",strtotime($day)),&#39;week_start_day&#39;=>strftime(&#39;%Y-%m-%d&#39;,strtotime($day)-($g-1)*86400),&#39;week_start_day_cn&#39;=>strftime(&#39;%Y年%m月%d日&#39;,strtotime($day)-($g-1)*86400),&#39;week_end_day&#39;=>strftime(&#39;%Y-%m-%d&#39;,strtotime($day) + (7-$g)*86400),&#39;week_end_day_cn&#39;=>strftime(&#39;%Y年%m月%d日&#39;,strtotime($day) + (7-$g)*86400));
    }
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to calculate the difference in weeks between two dates 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