PHP 怎么判断两个时间戳是不是在同一周?

假设 :  一星期开始是周一凌晨00:00 结束周日晚上00:00

解法1:  取其中一个时间,算出这个时间的周一和周日,然后跟另外一个时间对比,符合条件就算。(自己的思路)

<?php
header('Content-Type:text/html;charset=utf-8');
date_default_timezone_set('PRC');
 
    //第一个时间戳
    $one=1526978939;
    //第二个时间戳
    $two=1526978945;
 
    var_dump(run($one,$two));
  
    function run($one,$two){
        //拿第一个时间戳,计算周一和周日
 
        //周一
        $monday = strtotime('last Monday',$one);
        //周日
        $sunday = $monday+24*3600*7;
 
        //判断
        if($two>$sunday){
            return false;
        }
 
        if($two<=$monday){
            return false;
        }
        return true;
    }

解法二: 

用date函数可以确定当前时间戳是第几周,然后比较两个时间戳的数值。 (网友的思路

    //第一个时间戳
    $one=1526978939;
    //第二个时间戳
    $two=1526978945;
	
    $a=date('W',$one);
    $b=date('W',$two);
	
    //判断即可。

猜你喜欢

转载自blog.csdn.net/mrtwenty/article/details/80462578