use Carbon\Carbon;
use Carbon\CarbonPeriod;
function sanitizeTimeString(string $time): array
{
return str_split(str_replace(":", "", $time));
}
function isPerfectTime(string $time): bool
{
$exploded = sanitizeTimeString($time);
for ($i = 0; $i < count($exploded) - 1; $i++) {
$char = (int) $exploded[$i];
$nextChar = (int) $exploded[$i + 1];
if (!isExpectedChar($char, $nextChar)) {
return false;
}
}
return true;
}
function isExpectedChar(int $char, int $expected): bool
{
return $expected === $char + 1;
}
$arrayOfTimes = CarbonPeriod::create("today", "1 minute", "tomorrow")->map(
function (Carbon $date) {
return $date->format("g:i");
}
);
collect($arrayOfTimes)
->filter(fn(string $time) => isPerfectTime($time))
->unique();
// Illuminate\Support\Collection {#1429
// all: [
// 34 => "12:34",
// 83 => "1:23",
// 154 => "2:34",
// 225 => "3:45",
// 296 => "4:56",
// ],
// }