Last active
June 5, 2020 08:50
-
-
Save madsem/b21d862665eb1e4f88e586809af5e74a to your computer and use it in GitHub Desktop.
Create date ranges in sequence
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
/** | |
* Create a range of dates that are in sequence to properly mirror production data. | |
* | |
* @link https://carbon.nesbot.com/docs/#api-period | |
* | |
* @param $first | |
* @param $last | |
* @param int $resetAfter | |
* | |
* @return string | |
*/ | |
function dateRange($first, $last, $resetAfter = 30) | |
{ | |
static $dates = null; | |
static $counter = 0; | |
if ( ! is_array($dates) || $counter >= $resetAfter) { | |
$period = CarbonPeriod::create($first, $last)->toArray(); | |
$dates = explode(',',implode(',', $period)); | |
$counter = 0; | |
} | |
$counter++; | |
return array_shift($dates); | |
} | |
dateRange(Carbon::now()->subDays(30), Carbon::now()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good for Laravel factories, allows resetting the static date range array every XY iterations.