Skip to content

Instantly share code, notes, and snippets.

@jovialcore
Created September 13, 2024 16:25
Show Gist options
  • Save jovialcore/2d07686698c2fe262ff8dae2803c6447 to your computer and use it in GitHub Desktop.
Save jovialcore/2d07686698c2fe262ff8dae2803c6447 to your computer and use it in GitHub Desktop.
get the closest dates to a specified date and generate a range
<?php
private function getTimeline(array $dates, int $maxDays = 20)
{
$weekHolder = [];
$startDate = null;
foreach ($dates as $index => $date) {
if ($startDate === null) {
$startDate = $date;
}
$diff = $startDate->diffInDays($date);
if ($diff >= $maxDays || $index == count($dates) - 1) {
$weekHolder[] = [
'start_date' => $startDate->format('d M Y, H:i:s'),
'end_date' => $date->format('d M Y, H:i:s'),
];
}
$startDate = $date;
};
dd($weekHolder);
}
@jovialcore
Copy link
Author

PS: Dates are instance of a Carbon\Carbon

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