加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

如何在php中获得上周的日期范围?

发布时间:2020-12-13 13:46:40 所属栏目:PHP教程 来源:网络整理
导读:如何在php中获得上周的日期范围? 看我的代码如下: ?php function get_last_week_dates(){ // how can i get the date range last week ? // ex: today is 2014-2-8 // the week date range of last week should be '2014-1-26 ~ 2014-2-1' }? 你可以使用 s
如何在php中获得上周的日期范围?

看我的代码如下:

<?php 
    function get_last_week_dates(){
        // how can i get the date range last week ?
        // ex: today is 2014-2-8
        // the week date range of last week should be '2014-1-26 ~ 2014-2-1'
    }
?>
你可以使用 strtotime()
$previous_week = strtotime("-1 week +1 day");

$start_week = strtotime("last sunday midnight",$previous_week);
$end_week = strtotime("next saturday",$start_week);

$start_week = date("Y-m-d",$start_week);
$end_week = date("Y-m-d",$end_week);

echo $start_week.' '.$end_week ;

UPDATE

更改代码以处理星期日.如果当天是星期天,那么 – 1周将是上个星期日,并且再次获得上一个星期日,那将是一周之后.

$previous_week = strtotime("-1 week +1 day");

In addition if we need to find the current week and next week date
range we can do as

这个星期 –

$d = strtotime("today");
$start_week = strtotime("last sunday midnight",$d);
$end_week = strtotime("next saturday",$d);
$start = date("Y-m-d",$start_week); 
$end = date("Y-m-d",$end_week);

下周 –

$d = strtotime("+1 week -1 day");
$start_week = strtotime("last sunday midnight",$end_week);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读