Skip to content

Commit

Permalink
Add localization support to timeTense helper (#1063)
Browse files Browse the repository at this point in the history
Co-authored-by: Luke Towers <github@luketowers.ca>
Credit to @WebVPF
  • Loading branch information
WebVPF authored Feb 26, 2024
1 parent 552edb1 commit d9ff4e0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
22 changes: 9 additions & 13 deletions modules/system/helpers/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@
use DateTime as PhpDateTime;
use InvalidArgumentException;
use Exception;
use Lang;

class DateTime
{
/**
* Returns a human readable time difference from the value to the
* current time. Eg: **10 minutes ago**
*
* @return string
*/
public static function timeSince($datetime)
public static function timeSince($datetime): string
{
return self::makeCarbon($datetime)->diffForHumans();
}
Expand All @@ -22,28 +21,26 @@ public static function timeSince($datetime)
* Returns 24-hour time and the day using the grammatical tense
* of the current time. Eg: Today at 12:49, Yesterday at 4:00
* or 18 Sep 2015 at 14:33.
*
* @return string
*/
public static function timeTense($datetime)
public static function timeTense($datetime): string
{
$datetime = self::makeCarbon($datetime);
$yesterday = $datetime->subDays(1);
$tomorrow = $datetime->addDays(1);
$time = $datetime->format('H:i');
$date = $datetime->format('j M Y');
$date = $datetime->isoFormat('D MMM YYYY');

if ($datetime->isToday()) {
$date = 'Today';
$date = Lang::get('system::lang.datetime.today');
}
elseif ($datetime->isYesterday()) {
$date = 'Yesterday';
$date = Lang::get('system::lang.datetime.yesterday');
}
elseif ($datetime->isTomorrow()) {
$date = 'Tomorrow';
$date = Lang::get('system::lang.datetime.tomorrow');
}

return $date.' at '.$time;
return Lang::get('system::lang.datetime.at', ['date' => $date, 'time' => $time]);
}

/**
Expand Down Expand Up @@ -82,9 +79,8 @@ public static function makeCarbon($value, $throwException = true)
/**
* Converts a PHP date format to "Moment.js" format.
* @param string $format
* @return string
*/
public static function momentFormat($format)
public static function momentFormat($format): string
{
$replacements = [
'd' => 'DD',
Expand Down
6 changes: 6 additions & 0 deletions modules/system/lang/en/lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -470,4 +470,10 @@
'previous' => 'Previous',
'next' => 'Next',
],
'datetime' => [
'today' => 'Today',
'yesterday' => 'Yesterday',
'tomorrow' => 'Tomorrow',
'at' => ':date at :time',
],
];

0 comments on commit d9ff4e0

Please sign in to comment.