Skip to content

Commit

Permalink
Added Date.php from Steam 250.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilge committed Aug 25, 2020
2 parents 32e1fe8 + b5ca169 commit e5eb26c
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/Generate/Date.php
@@ -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";
}
}

0 comments on commit e5eb26c

Please sign in to comment.