Created
August 9, 2013 00:19
-
-
Save hailwood/6190075 to your computer and use it in GitHub Desktop.
Get the bounds for the next, and following month in php
This file contains 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
//Get the current Month/Year | |
$currentMonth = date('n'); | |
$currentYear = date('Y'); | |
//Add one to the current month accounting for the possibility of end of year | |
if($currentMonth == 12){ | |
$nextMonth = 1; | |
$nextYear = $currentYear+1; | |
} else { | |
$nextMonth = $currentMonth+1; | |
$nextYear = $currentYear; | |
} | |
//Pad the date so it's valid | |
$yearPadded = str_pad($nextYear, 2, "0", STR_PAD_LEFT); | |
$monthPadded = str_pad($nextMonth, 2, "0", STR_PAD_LEFT); | |
//And now we have next month | |
$nextMonthStart = $yearPadded.'-'.$monthPadded.'-01'; | |
$nextMonthEnd = date('Y-m-t', strtotime($nextMonthStart)); | |
//Add one to the incremented month accounting for the possibility of end of year | |
if($nextMonth == 12){ | |
$followingMonth = 1; | |
$followingYear = $nextYear+1; | |
} else { | |
$followingMonth = $nextMonth+1; | |
$followingYear = $nextYear; | |
} | |
//Pad the date so it's valid | |
$yearPadded = str_pad($followingYear, 2, "0", STR_PAD_LEFT); | |
$monthPadded = str_pad($followingMonth, 2, "0", STR_PAD_LEFT); | |
//And now we have the following month | |
$followingMonthStart = $yearPadded.'-'.$monthPadded.'-01'; | |
$followingMonthEnd = date('Y-m-t', strtotime($followingMonthStart)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment