From d9f48a95502642ab30396be4c31ea7d2278d4a65 Mon Sep 17 00:00:00 2001 From: Jonny McCullagh Date: Sun, 25 Oct 2020 19:15:39 +0000 Subject: [PATCH 1/4] Add diffForHumans function --- src/Timezone.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Timezone.php b/src/Timezone.php index 7e9c9d9..7877e1f 100644 --- a/src/Timezone.php +++ b/src/Timezone.php @@ -35,6 +35,25 @@ public function convertToLocal(?Carbon $date, $format = null, $format_timezone = return $formatted_date_time; } + /** + * @param Carbon|null $date + * @return string + */ + public function convertToLocalDiffForHumans(?Carbon $date) : string + { + if (is_null($date)) { + return 'Empty'; + } + + $timezone = (auth()->user()->timezone) ?? config('app.timezone'); + + $date->setTimezone($timezone); + + $diffTime = Carbon::parse($date)->diffForHumans(); + + return $diffTime; + } + /** * @param $date * @return Carbon From 1f75cdd45321dffbbc62fdad1bef50ea60f03f90 Mon Sep 17 00:00:00 2001 From: Jonny McCullagh Date: Fri, 6 Nov 2020 22:23:22 +0000 Subject: [PATCH 2/4] style ci fixes --- src/Timezone.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Timezone.php b/src/Timezone.php index 7877e1f..606c0b9 100644 --- a/src/Timezone.php +++ b/src/Timezone.php @@ -12,7 +12,7 @@ class Timezone * @param bool $format_timezone * @return string */ - public function convertToLocal(?Carbon $date, $format = null, $format_timezone = false) : string + public function convertToLocal(?Carbon $date, $format = null, $format_timezone = false): string { if (is_null($date)) { return 'Empty'; @@ -29,7 +29,7 @@ public function convertToLocal(?Carbon $date, $format = null, $format_timezone = $formatted_date_time = $date->format($format); if ($format_timezone) { - return $formatted_date_time . ' ' . $this->formatTimezone($date); + return $formatted_date_time.' '.$this->formatTimezone($date); } return $formatted_date_time; @@ -39,7 +39,7 @@ public function convertToLocal(?Carbon $date, $format = null, $format_timezone = * @param Carbon|null $date * @return string */ - public function convertToLocalDiffForHumans(?Carbon $date) : string + public function convertToLocalDiffForHumans(?Carbon $date): string { if (is_null($date)) { return 'Empty'; @@ -58,7 +58,7 @@ public function convertToLocalDiffForHumans(?Carbon $date) : string * @param $date * @return Carbon */ - public function convertFromLocal($date) : Carbon + public function convertFromLocal($date): Carbon { return Carbon::parse($date, auth()->user()->timezone)->setTimezone('UTC'); } @@ -67,13 +67,13 @@ public function convertFromLocal($date) : Carbon * @param Carbon $date * @return string */ - private function formatTimezone(Carbon $date) : string + private function formatTimezone(Carbon $date): string { $timezone = $date->format('e'); $parts = explode('/', $timezone); if (count($parts) > 1) { - return str_replace('_', ' ', $parts[1]) . ', ' . $parts[0]; + return str_replace('_', ' ', $parts[1]).', '.$parts[0]; } return str_replace('_', ' ', $parts[0]); From d0fc5f726c0987b3c7f953ad4d759bec4fc21ac7 Mon Sep 17 00:00:00 2001 From: James Mills Date: Thu, 18 Feb 2021 10:30:35 +0400 Subject: [PATCH 3/4] Removing duplicate code. Making use of current function internally. --- src/Timezone.php | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/Timezone.php b/src/Timezone.php index 606c0b9..f1403e2 100644 --- a/src/Timezone.php +++ b/src/Timezone.php @@ -12,7 +12,7 @@ class Timezone * @param bool $format_timezone * @return string */ - public function convertToLocal(?Carbon $date, $format = null, $format_timezone = false): string + public function convertToLocal(?Carbon $date, $format = null, $format_timezone = false, $diff_for_humans = false): string { if (is_null($date)) { return 'Empty'; @@ -26,6 +26,10 @@ public function convertToLocal(?Carbon $date, $format = null, $format_timezone = return $date->format(config('timezone.format')); } + if ($diff_for_humans) { + return $date->diffForHumans(); + } + $formatted_date_time = $date->format($format); if ($format_timezone) { @@ -41,17 +45,7 @@ public function convertToLocal(?Carbon $date, $format = null, $format_timezone = */ public function convertToLocalDiffForHumans(?Carbon $date): string { - if (is_null($date)) { - return 'Empty'; - } - - $timezone = (auth()->user()->timezone) ?? config('app.timezone'); - - $date->setTimezone($timezone); - - $diffTime = Carbon::parse($date)->diffForHumans(); - - return $diffTime; + return $this->convertToLocal($date, null, false, true); } /** From b8268c74b8ecca78c28ca6daca6aedf9ea4b21af Mon Sep 17 00:00:00 2001 From: James Mills Date: Thu, 18 Feb 2021 10:32:14 +0400 Subject: [PATCH 4/4] Coding style update --- src/Timezone.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Timezone.php b/src/Timezone.php index f1403e2..2128fd7 100644 --- a/src/Timezone.php +++ b/src/Timezone.php @@ -33,7 +33,7 @@ public function convertToLocal(?Carbon $date, $format = null, $format_timezone = $formatted_date_time = $date->format($format); if ($format_timezone) { - return $formatted_date_time.' '.$this->formatTimezone($date); + return $formatted_date_time . ' ' . $this->formatTimezone($date); } return $formatted_date_time; @@ -67,7 +67,7 @@ private function formatTimezone(Carbon $date): string $parts = explode('/', $timezone); if (count($parts) > 1) { - return str_replace('_', ' ', $parts[1]).', '.$parts[0]; + return str_replace('_', ' ', $parts[1]) . ', ' . $parts[0]; } return str_replace('_', ' ', $parts[0]);