generated from ScriptFUSION/Template
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
1 changed file
with
40 additions
and
0 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,40 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace ScriptFUSION\Steam250\SiteGenerator\Generate; | ||
|
||
use ScriptFUSION\StaticClass; | ||
|
||
final class Date | ||
{ | ||
use StaticClass; | ||
|
||
/** | ||
* Adaptively transforms the specified date, producing a relative or absolute date depending upon how recent it is. | ||
* | ||
* @param string|\DateTimeInterface $date Date. | ||
* | ||
* @return string Absolute or relative date. | ||
*/ | ||
public static function adapt($date): string | ||
{ | ||
$date = is_string($date) ? new \DateTimeImmutable($date) : $date; | ||
$diff = $date->diff(new \DateTime); | ||
|
||
// Absolute. | ||
if ($diff->days > 30) { | ||
return $date->format('M Y'); | ||
} | ||
|
||
// Relative. | ||
switch ($diff->days) { | ||
case 0: | ||
return 'today'; | ||
|
||
case 1: | ||
return 'yesterday'; | ||
} | ||
|
||
return "$diff->days days"; | ||
} | ||
} |