Skip to content

Instantly share code, notes, and snippets.

@dannetstudio
Created October 6, 2022 18:02
Show Gist options
  • Select an option

  • Save dannetstudio/51074056215960767f08e92da285e892 to your computer and use it in GitHub Desktop.

Select an option

Save dannetstudio/51074056215960767f08e92da285e892 to your computer and use it in GitHub Desktop.
Laravel Blade Component for Dates with the user’s timezone
<?php
namespace App\View\Components;
use App\Helpers\Helpers;
use Carbon\Carbon;
use Illuminate\View\Component;
class DateTimeZone extends Component
{
public Carbon $date;
public mixed $format;
/**
* Create a new component instance.
*
* @return void
*/
public function __construct(Carbon $date, $format = null)
{
$this->date = $date->setTimezone(Helpers::getUserTimeZone());
$this->format = $format;
}
protected function format()
{
return $this->format ?? 'Y-m-d H:i:s';
}
/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|\Closure|string
*/
public function render()
{
return $this->date->format($this->format());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment