S
S
Serverprom2015-09-18 12:07:19
PHP
Serverprom, 2015-09-18 12:07:19

How to get dates of all days of this and next week in PHP?

Hello! There was a need to get dates (not timestamp) of all days of this and next week in PHP. Can you tell me how this can be done?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Safonov, 2015-09-18
@Serverprom

On the knee

function getDatesForThisAndNextWeek()
{
  $days = ['Monday' => 1, 'Tuesday' => 2, 'Wednesday' => 3, 'Thursday' => 4, 'Friday' => 5, 'Saturday' => 6, 'Sunday' => 7];

  $i = 1;
  foreach($days as $k => $v)
  {
    $today = new \DateTime();
    $today->setISODate((int)$today->format('o'), (int)$today->format('W'), $days[ ucfirst($k) ]);
    $daysArray[ $i ] = $today->format('Y-m-d');

    $todayPlusWeek       = $today->modify('+1 week');
    $daysArray[ $i + 7 ] = $todayPlusWeek->format('Y-m-d');

    $i++;
  }
  ksort($daysArray);
  return $daysArray;
}

var_dump(getDatesForThisAndNextWeek());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question