>  기사  >  백엔드 개발  >  PHP에서 두 날짜 간의 주 차이를 계산하는 방법

PHP에서 두 날짜 간의 주 차이를 계산하는 방법

藏色散人
藏色散人원래의
2021-07-27 09:03:361793검색

PHP에서 두 날짜 사이의 주 수를 계산하는 방법: 먼저 PHP 샘플 파일을 만든 다음 "함수 형식($a,$b){...}" 및 기타 방법을 사용하여 사이의 주 수를 계산합니다. 두 날짜, 즉 Can입니다.

PHP에서 두 날짜 간의 주 차이를 계산하는 방법

이 기사의 운영 환경: Windows 7 시스템, PHP 버전 7.1, DELL G3 컴퓨터

PHP는 두 날짜 간의 주 단위 차이를 어떻게 계산합니까?

PHP를 사용하여 두 날짜 사이의 연, 월, 주, 일 수를 계산합니다.

코드는 다음과 같습니다.

<?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;));
?>

실행 결과:

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

php 특정 날짜가 몇 주에 속하는지 쿼리합니다. 및 해당 주의 시작일

코드는 다음과 같습니다.

/**
* @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));
    }

추천 학습: "PHP Video Tutorial"

위 내용은 PHP에서 두 날짜 간의 주 차이를 계산하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.