Skip to content

Instantly share code, notes, and snippets.

@madsem
Last active June 5, 2020 08:50
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 madsem/b21d862665eb1e4f88e586809af5e74a to your computer and use it in GitHub Desktop.
Save madsem/b21d862665eb1e4f88e586809af5e74a to your computer and use it in GitHub Desktop.
Create date ranges in sequence
/**
* 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());
@madsem
Copy link
Author

madsem commented Jun 5, 2020

Good for Laravel factories, allows resetting the static date range array every XY iterations.

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