PHP怎样获得最近一个周一和上周一的日期

PHP怎样获得最近一个周一和上周一的日期,第1张

本周周一的日期:

$ts = time();

$this_monday = this_monday($ts);

echo date('Y-m-d H:i:s', $this_monday);

//这个星期的星期一  

// @$timestamp ,某个星期的某一个时间戳,默认为当前时间  

// @is_return_timestamp ,是否返回时间戳,否则返回时间格式  

function this_monday($timestamp=0,$is_return_timestamp=true){  

    static $cache ;  

    $id = $timestamp$is_return_timestamp;  

    if(!isset($cache[$id])){  

        if(!$timestamp) $timestamp = time();  

        $monday_date = date('Y-m-d', $timestamp-86400date('w',$timestamp)+(date('w',$timestamp)>086400:-/686400/518400));  

        if($is_return_timestamp){  

            $cache[$id] = strtotime($monday_date);  

        }else{  

            $cache[$id] = $monday_date;  

        }  

    }  

    return $cache[$id];  

   

}

上周一的函数则为

$ts = time();

$last_monday = last_monday($ts);

echo date('Y-m-d H:i:s', $last_monday);

//上周一  

// @$timestamp ,某个星期的某一个时间戳,默认为当前时间  

// @is_return_timestamp ,是否返回时间戳,否则返回时间格式  

function last_monday($timestamp=0,$is_return_timestamp=true){  

    static $cache ;  

    $id = $timestamp$is_return_timestamp;  

    if(!isset($cache[$id])){  

        if(!$timestamp) $timestamp = time();  

        $thismonday = this_monday($timestamp) - /786400/604800;  

        if($is_return_timestamp){  

            $cache[$id] = $thismonday;  

        }else{  

            $cache[$id] = date('Y-m-d',$thismonday);  

        }  

    }  

    return $cache[$id];  

}

/今天/

select  from 表名 where to_days(时间字段) = to_days(now());

/昨天/

select  from 表名 where to_days(now())-to_days(时间字段) = 1;

/近7天/

select  from 表名 where date_sub(curdate(), interval 7 day) <= date(时间字段);

/查询距离当前现在6个月的数据/

select  from 表名 where 时间字段 between date_sub(now(),interval 6 month) and now();

/查询当前这周的数据/

select  from 表名 where yearweek(date_format(时间字段,'%Y-%m-%d')) = yearweek(now());

/查询上周的数据/

select  from 表名 where yearweek(date_format(时间字段,'%Y-%m-%d')) = yearweek(now())-1;

/查询当前月份的数据/

select  from 表名 where date_format(时间字段,'%Y-%m')=date_format(now(),'%Y-%m');

/查询上个月的数据/

select  from 表名 where date_format(时间字段,'%Y-%m')=date_format(date_sub(curdate(), interval 1 month),'%Y-%m');

其它获取类似以上的代码显示

$time=time();

$week=date("N",$time);

switch ($week) {

case 1:

# code

echo "今天是星期一";

break;

case 2:

# code

echo "今天是星期二";

break;

case 3:

echo "今天是星期三";

break;

case 4:

echo "今天是星期四";

break;

case 5:

echo "今天是星期五";

break;

case 6:

echo "今天是星期六";

break;

default:

echo "今天是星期日";

break;

}

//亲测有效,不会再问\(^V^)/

自己理了一下思路,因为我做的时候sql语句里用的是小于和大于,所以直接统计到下一天的0点。最后返回的是数组。

//开始时间

$startDate = "2013-12-12";

//结束时间

$endDate = "2013-12-16";

//跨越天数

$n = (strtotime($endDate)-strtotime($startDate))/86400;

//结束时间加一天(sql语句里用的是小于和大于,如果有等于的话这句可以不要)

$endDate = date("Y-m-d 00:00:00",strtotime("$endDate +1 day"));

//判断,跨度小于7天,可能是同一周,也可能是两周

if($n<7){

//查开始时间 在 那周 的 位置

$day = date("w",strtotime($startDate))-1;

//查开始时间 那周 的 周一

$week_start = date("Y-m-d 00:00:00",strtotime("$startDate -{$day} day"));

//查开始时间 那周 的 周末

$day = 7-$day;

$week_end = date("Y-m-d 00:00:00",strtotime("$startDate +{$day} day"));

//判断周末时间是否大于时间段的结束时间,如果大于,那就是时间段在同一周,否则时间段跨两周

if($week_end>=$endDate){

$weekList[] =array($startDate,$endDate);

}else{

$weekList[] =array($startDate,$week_end);

$weekList[] =array($week_end,$endDate);

}

}else{

//如果跨度大于等于7天,可能是刚好1周或跨2周或跨N周,先找出开始时间 在 那周 的 位置和那周的周末时间

$day = date("w",strtotime($startDate))-1;

$week_start = date("Y-m-d 00:00:00",strtotime("$startDate -{$day} day"));

$day = 7-$day;

$week_end = date("Y-m-d 00:00:00",strtotime("$startDate +{$day} day"));

//先把开始时间那周写入数组

$weekList[] =array($startDate,$week_end);

//判断周末是否大于等于结束时间,不管大于(2周)还是等于(1周),结束时间都是时间段的结束时间。

if($week_end >= $endDate){

$weekList[] = array($week_end,$endDate);

}else{

//N周的情况用while循环一下,然后写入数组

while($week_end <= $endDate){

$start = $week_end;

$week_end = date("Y-m-d 00:00:00",strtotime("$week_end +7 day"));

if($week_end <= $endDate){

$weekList[] = array($start,$week_end);

}else{

$weekList[] = array($start,$endDate);

}itjob

}

}

}

以上就是关于PHP怎样获得最近一个周一和上周一的日期全部的内容,包括:PHP怎样获得最近一个周一和上周一的日期、如何用PHP 获取今天之前,本周之前,本月之前,本年之前,今天,本周,本月,本年的数据呢、php获取星期几等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9488786.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-28
下一篇2023-04-28

发表评论

登录后才能评论

评论列表(0条)

    保存