Skip to content

Instantly share code, notes, and snippets.

@muthu32
Last active January 2, 2018 13:25
Show Gist options
  • Save muthu32/b9b7a69cb3b89b5945778bdbce31839b to your computer and use it in GitHub Desktop.
Save muthu32/b9b7a69cb3b89b5945778bdbce31839b to your computer and use it in GitHub Desktop.
php get week range
<?php
function weekrange($year, $i = 0)
{
$week = sprintf("%02d", $i);
$dto = new DateTime();
$dto->setISODate($year, $week);
$year2 = $dto->format('Y');
if ($year2 < $year) $st = '01-';
else $st = $dto->format('d') . "-";
$dto->modify('+6 days');
$year2 = $dto->format('Y');
if ($year2 < $year)
{
++$i;
return weekrange($year, $i);
}
$st.= $dto->format('d/m');
$week = array();
$week[] = $st;
$dto->modify('+1 days');
while ($i < 54)
{
$st = $dto->format('d') . "-";
if ($dto->format('Y') > $year) break;
$dto->modify('+6 days');
if ($dto->format('Y') > $year)
{
$st.= '31/12';
$week[] = $st;
break;
}
else
{
$st.= $dto->format('d/m');
$dto->modify('+1 days');
$week[] = $st;
}
++$i;
}
return $week;
}
$range = weekrange(2018);
echo "<pre>";
print_r($range);
echo "</pre>";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment