Last active
August 22, 2017 13:06
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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