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

前端之家收集整理的这篇文章主要介绍了如何在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'
    }
?>
你可以使用 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);

猜你在找的PHP相关文章