Created
October 6, 2022 18:02
-
-
Save dannetstudio/51074056215960767f08e92da285e892 to your computer and use it in GitHub Desktop.
Laravel Blade Component for Dates with the user’s timezone
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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