Skip to content

Instantly share code, notes, and snippets.

@soifou
Last active August 22, 2017 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soifou/36b819c8bca6dc59c3813a2d783664a9 to your computer and use it in GitHub Desktop.
Save soifou/36b819c8bca6dc59c3813a2d783664a9 to your computer and use it in GitHub Desktop.
Display first/last date of the month from current month during 12 months
<?php
// display first/last date of the month from current month during one year
for ($monthToDisplay = 0; $monthToDisplay < 12; $monthToDisplay++)
{
$f = new \DateTime('first day of this month');
$l = new \DateTime('last day of this month');
$df = new \DateInterval('P'.$monthToDisplay.'M');
$dl = new \DateInterval('P1M');
$first = $f->add($df)->format('d/m/Y');
$last = $f->add($dl)->sub(new DateInterval('P1D'))->format('d/m/Y');
echo $first . " - " . $last . "\n";
}
// will display
// 01/01/2017 - 31/01/2017
// 01/02/2017 - 28/02/2017
// 01/03/2017 - 31/03/2017
// 01/04/2017 - 30/04/2017
// ...
// 01/12/2017 - 31/12/2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment