Skip to content

Instantly share code, notes, and snippets.

@Gummibeer
Created May 19, 2022 18:49
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 Gummibeer/72a5c96242b88ca66610e3b8f6503030 to your computer and use it in GitHub Desktop.
Save Gummibeer/72a5c96242b88ca66610e3b8f6503030 to your computer and use it in GitHub Desktop.
<?php
use Carbon\Carbon;
use Carbon\CarbonPeriod;
use Illuminate\Support\Collection;
$period = collect(CarbonPeriod::since(Carbon::parse('-1year'))->days(1)->until(Carbon::now()))
->concat(CarbonPeriod::since(Carbon::parse('-1week'))->hours(1)->until(Carbon::now()))
->concat(CarbonPeriod::since(Carbon::parse('-1day'))->minutes(30)->until(Carbon::now()));
$backupsPerPeriod = [];
$backupsPerPeriod['daily'] = $period->groupBy(fn (Carbon $date) => $date->format('Ymd'));
$backupsPerPeriod['weekly'] = $period->groupBy(fn (Carbon $date) => $date->format('YW'));
$backupsPerPeriod['monthly'] = $period->groupBy(fn (Carbon $date) => $date->format('Ym'));
$backupsPerPeriod['yearly'] = $period->groupBy(fn (Carbon $date) => $date->format('Y'));
collect($backupsPerPeriod)
->map(
fn(Collection $periods) => $periods
->map(
fn(Collection $backups) => $backups
->map->toDateTimeString()
->unique()
->sortDesc()
->values()
)
->sortKeysDesc()
)
->toArray();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment