登录  /  注册

php怎么计算两个日期相差几周

藏色散人
发布: 2021-07-27 09:03:36
原创
1615人浏览过

php计算两个日期相差几周的方法:首先创建一个php示例文件;然后通过“function format($a,$b){...}”等方法实现计算两个日期间隔的周数即可。

php怎么计算两个日期相差几周

本文操作环境:windows7系统、PHP7.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视频教程

以上就是php怎么计算两个日期相差几周的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号