Skip to content

Instantly share code, notes, and snippets.

@ingebrixen
Last active November 8, 2022 09:42
Show Gist options
  • Save ingebrixen/a278bf3c17ab3c9e8073826d784c901a to your computer and use it in GitHub Desktop.
Save ingebrixen/a278bf3c17ab3c9e8073826d784c901a to your computer and use it in GitHub Desktop.
getDateRange
public function getDateRange()
{
$Start = new \DateTime(date('Y-m-d')); // Datum von
$dtEnd = new \DateTime(date('m/y')); // Datum bis
$dtEnd->modify('first day of next month');
$dtStart = $Start->modify('-1 year');
$period = new \DatePeriod(
$dtStart,
new \DateInterval('P1M'), // Periode: 1 Monat
$dtEnd
);
$period = array_reverse(iterator_to_array($period));
return $period;
}
@ingebrixen
Copy link
Author

ingebrixen commented Sep 28, 2021

Method to dynamically create a one-year DateRange based on the current month. I use this on my website as a dropdown menu filter to show only the monthly dates selected by the user.

creates:
2021/09
2021/08
2021/07
2021/06
2021/05
2021/04
2021/03
2021/02
2021/01
2020/12
2020/11
2020/10
2020/09

based on the actuall month

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment