Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pullrequests/jonnymccullagh/diff for humans #63

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/Timezone.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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) {
Expand All @@ -35,11 +39,20 @@ 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
{
return $this->convertToLocal($date, null, false, true);
}

/**
* @param $date
* @return Carbon
*/
public function convertFromLocal($date) : Carbon
public function convertFromLocal($date): Carbon
{
return Carbon::parse($date, auth()->user()->timezone)->setTimezone('UTC');
}
Expand All @@ -48,7 +61,7 @@ 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);
Expand Down