kkochanski kkochanski - 1 month ago 24
PHP Question

PHP date - get beginning and ending day of given week from format W-m-Y

I have an array of dates in format

W-m-Y
.

From e.g.
34-08-2016
I would like to get something like
20-08-2016 - 26-08-2016
. Those days from requested format, aren't real.

Any idea how to tackle it?

Answer

try this

function getStartAndEndDate($week, $year) {
  $dto = new DateTime();
  $dto->setISODate($year, $week);
  $ret['week_start'] = $dto->format('Y-m-d');
  $dto->modify('+6 days');
  $ret['week_end'] = $dto->format('Y-m-d');
  return $ret;
}

$week_array = getStartAndEndDate(34,2016);

echo "start date ".date('d-m-Y',strtotime($week_array['week_start'])).'<br>';
echo "End date ".date('d-m-Y',strtotime($week_array['week_end']));