Search code examples
phplaravelphp-carbon

Displaying timeago in laravel in the view


I am using laravel and I have a foreach loop in my blade view where I display all the players from the database. I am saving their property birthday as a string in this kind of format 30/11/1994. Instead of showing their birthday in that kind of format, what is the easiest way of displaying the date and timeago of each the player in the view in the format like this:

born Nov 30, 1994, 22 years old 

Solution

  • Using Laravel Carbon

    $birthday = Carbon::createFromFormat('d-m-Y', $birthday);
    
    //in blade
    Born on {{ $birthday->toFormattedDateString() }}, {{ $birthday->age }} years old
    
    //will display 'Born on Nov 30, 1994, 22 Years old