how can I get last week' date range in php?

date, php

Solution

You can use 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

Changed the code to handle sunday. If the current day is sunday then - 1 week will be previous sunday and again getting previous sunday for that will go the one week back.

$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

Current week -

$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);  

Next Week -

$d = strtotime("+1 week -1 day");
$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); 

Problem

how can I get last week' date range in php ? see my codes bellow: ``` <?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' } ?> ```

Original source