How to convert database timestamp to fill a datetime-local field?

Asked

Viewed 379 times

3

I need to fill in a field:

<input id="date" type="datetime-local" class="form-control" value="{{$task->date}}" name="date">

The return comes in format timestamp ("2016-09-20 10:00:00"), is there any helper of Larable that converts the format?

  • I don’t usually wear a lot, but I know the Laravel is kind of integrated with the Carbon that in the few times I needed it made it much easier.

  • @Neuberoliveira yes Carbon helps a lot, but right now I just need to convert the format to fill in a local datetime field :/

3 answers

3


I got it, I used it that way:

<input id="date" type="datetime-local" class="form-control"
                                   value="{{date("Y-m-d\TH:i:s", strtotime($task->date))}}"
                                   name="date" placeholder="Data">
  • Nice to have found the solution and share +1. A tip I give you is to create a helper of your own Laravel and decrease this huge code in View. Congratulations ...

  • 1

    @Virgilionovic thanks for the tip, I’ll try to apply ^^

1

I input of the type date HTML5 only accepts one type of formatting, which is AAAA/mm/dd that is to say 2016-09-19, knowing this you can do as follows.

<input type="date" class="form-control" value="{{ date('Y-m-d H:i:s', strtotime($task->date)) }}">
  • It is type datetime-local or Y/m/d H:i:s, but even using date('Y-m-d H:i:s', strtotime($task->date)) did not work :/

  • use the same principle, changed again.

  • Gave in the same friend, had already tried it :/

1

Just to illustrate I think it would be something like:

<input id="date" type="datetime-local" class="form-control" value="{{Carbon::createFromFormat('Y/m/d H:i:s', $task->date)->toDateTimeString()}}" name="date">
  • I don’t know if it is possible to use Carbon in the view, but anyway forced to try, I used it that way and it worked value="{date("Y-m-d TH:i:s", strtotime($task->date))}}"

Browser other questions tagged

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