'Different date between sql and view
here is my problem I want to display in my view a date in the format 'Y-d-m H:i:s.u', it is inserted correctly in my database but when it is displayed in the view 2 years were added and days too.dd of my tickets
In my controller
{
$tickets = Ticket::where('user_id', Auth::user()->id)->paginate(10);
// dd($tickets);
return view('tickets.user_tickets', compact('tickets'));
}
In my view
<td> {{ $ticket->created_at }}</td>
Thank you to all those who can help me
Solution 1:[1]
Use PHP date function to display in view:
date('F,d Y h:i:s' , strtotime($ticket->created_at))
Solution 2:[2]
$ticket->created_at should be a Carbon instance, meaning you can format it as required via:
{{ $ticket->created_at->format('Y-d-m H:i:s.u') }}
Check the documentation:
https://carbon.nesbot.com/docs/#api-setters
If this doesn't work, make sure your Ticket.php model has it specified:
<?php
namespace App\Models;
class Ticket extends Model {
protected $dates = ['created_at', 'updated_at'];
}
Anything in the protected $dates can chain the ->format() method, or any other Carbon method.
Solution 3:[3]
Try it in your view
<td>{{ date('Y-d-m H:i:s', strtotime($ticket->created_at)) }}</td>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Faizan Ali |
| Solution 2 | Tim Lewis |
| Solution 3 | poppies |
