I need to format date for Brazil

Asked

Viewed 896 times

-2

Hello,

I need to show the date 00-00-0000 as I do in this code below?

<span data-bind="text: created_at"></span>

I appreciate any help

1 answer

2

To use this date pattern, you can use PHP’s native function, the Date or the Carbon (standard class that Laravel uses for date), which is already installed in Laravel.

In the control use as example the code below. Add $user->created_at->format("d-m-Y"). Ex:

public function index()
{        
    $reviews = \App\Reviews::all();

    return view('home', ["reviews" => $reviews]);
}

In the view use

@foreach ($reviews as $review)
<span data-bind="text: {{ $review->created_at->format("d-m-Y") }}"></span>
@endforeach

If you want to change the format, simply replace d-m-Y by any of the following table values: http://br.php.net/manual/en/function.date.php

  • Hello Valdeir, thank you. but giving the following error Undefined variable: created_at

  • Are you using Lade? You are passing the variable created_at in an array for the view? If possible edit your question with snippets of your code.

  • Yes, Blade , I put it as directed public Function index() { $users = Reviews:::all(); Return view('user.index', pact('Reviews')); $user = App User::find(1); $data["created_at"] = $user->created_at->format("d-m-Y"); Return view('home', $data); } in Blade <span data-bind="text: {{ $created_at }}></span>

  • @Toninho, I changed my example.

  • Undefined variable: Views

  • @Toninho, you need to adapt my example to your code. As I do not have the source code of your project, I can only analyze superficially. If possible edit your question with part of its source code. This error is in view, controller... ?

  • I put in the index.blade.php view just like you gave me. The error is in index.blade.php

  • I changed my answer, if possible check again.

  • part of index.Blade <Section data-bind="foreach: {data: sourceItems, afterRender: lazyLoadImage}" class="col-Sm-8"> Published: <span data-bind="text: created_at"></span><br>

  • Undefined variable: Views

  • Posta his view in your question (and not just a line from it), but it is difficult. It is so much that you are probably using knockoutjs (or similar) and did not make it clear in the question. Only assimilei now. Without information there is much to help.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.