Kyle
Kyle

Reputation: 793

Return Monday to Sundays of Current Week

I was wondering how to grab from Monday (03-12-2018) to Sundays (03-18-2018) dates. I know that if I use 'this sunday' it will return Sundays dates but how would I do that for all the rest? Using this on every week day would result in Monday and Tuesday (From this standing point) to be 19 and 20.

date_default_timezone_set('America/New_York');
$the_date = date('Y-m-d', strotime("this sunday"));

Upvotes: 0

Views: 43

Answers (1)

Song
Song

Reputation: 301

Try this:

date_default_timezone_set('America/New_York');

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

$sunday =  date('Y-m-d', strtotime("sunday this week"));

$period = new DatePeriod(
     new DateTime($monday),
     new DateInterval('P1D'),
     (new DateTime($sunday))->modify('+1 day')
);

foreach ($period as $date) {
    echo $date->format('Y-m-d'), "\n";
}

Demo https://implode.io/9qLW1K

Upvotes: 2

Related Questions