-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2a01809
commit 0154141
Showing
3 changed files
with
47 additions
and
16 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use Illuminate\Support\Carbon; | ||
|
||
class ExtendedCarbon extends Carbon | ||
{ | ||
/** | ||
* Get the year and week formatted as a string in the ISO 8601 week date format. | ||
* | ||
* @return string The formatted year-week string. | ||
* ``` | ||
* '2024-W25' | ||
* ``` | ||
*/ | ||
public function yearWeekString(): string | ||
{ | ||
if ($this->week < 10 && !str_contains($this->week, '0')) { | ||
$week = "0$this->week"; | ||
} | ||
return "$this->year-W$week"; | ||
} | ||
|
||
/** | ||
* Get the closest date and time from `$this` in the array of dates. | ||
* | ||
* @param array<Carbon|ExtendedCarbon|string> $dates An array of dates to compare against the target. | ||
* @return ExtendedCarbon|null The closest date and time from the array compared to the target. | ||
*/ | ||
public function closestFromDateArray(array $dates) | ||
{ | ||
$closest = null; | ||
foreach ($dates as $date) { | ||
$current = ExtendedCarbon::parse($date); | ||
if (is_null($closest)) { | ||
$closest = $current; | ||
continue; | ||
} | ||
$closest = $this->closest($closest, $current) === $current ? $current : $closest; | ||
} | ||
return $closest; | ||
} | ||
} |
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
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