STRTOTIME to find current week, date for Monday and Saturday, PHP

Viewed 23124

I have these lines:

$staticstart = date('Y-m-d',strtotime('last Monday'));
$staticfinish = date('Y-m-d',strtotime('next Saturday'));

And I am using them to select the monday and saturday of the current week, But when it is actually Monday, it choses the monday of the previous week, thus showing 2 weeks of data.... I have tried this and it produces no result:

$staticstart = date('Y-m-d',strtotime('this Monday'));

What have I missed? Is there a better way to find the monday and Saturday (dates) of the current week?

6 Answers

To get the date range between current week

$monday = strtotime("last monday");
$monday = date('w', $monday)==date('w') ? $monday+7*86400 : $monday;

$sunday = strtotime(date("Y-m-d",$monday)." +6 days");

$this_week_start = date("Y-m-d",$monday);
$this_week_end = date("Y-m-d",$sunday);

echo "Current week range from $this_week_start to $this_week_end ";

I think this is a simpler solution

$monday = date('Y-m-d', strtotime('monday this week'));
$saturday = date('Y-m-d', strtotime('saturday this week'));
Related