Created
March 20, 2022 19:48
-
-
Save mallardduck/d54597ecb9359b86a80d8d7eb66a5611 to your computer and use it in GitHub Desktop.
Laravel Human date time diff example
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
<x-human-date-time-diff :date-time="$post->posted_at"></x-human-date-time-diff> |
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
<time datetime="{{ $dateTime->toW3cString() }}" title="{{ $dateTime->format('Y-m-d \a\t H:i A O') }}">{{ $dateTime->diffForHumans() }}</time> |
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 Carbon\Carbon; | |
use Illuminate\View\Component; | |
class HumanDateTimeDiff extends Component | |
{ | |
public $dateTime; | |
/** | |
* Create a new component instance. | |
* | |
* @param Carbon $dateTime | |
*/ | |
public function __construct(Carbon $dateTime) | |
{ | |
$this->dateTime = $dateTime; | |
} | |
/** | |
* Get the view / contents that represent the component. | |
* | |
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View | |
*/ | |
public function render(): \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory | |
{ | |
return view('components.human-date-time-diff'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome! I recall doing something similar too. I love the Time element and usually also use a human readable time as a title.
To make it even shorter just using
<x-human-date-time-diff :date-time="$post->posted_at" />
is enough!