Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

johndoee's avatar

how can show created_at updated_at value as only date and time in blade file

how can i show date time value from database in blade file in database , created_at value is

2022-12-20 13:20:51

when i show in blade file , it show as

2022-12-20T13:20:51.000000Z

here is controller

  public function sellpending(){
       
    
        $sellpendingdata = LimitSell ::where('user_id',Auth::user()->id)-> where('amount', '!=', 0)->get();
        return response()->json([
         'sellpendingdata' => $sellpendingdata,
        
        ]);
    }

here is blade file

  function sellpendingshow() {
                     
                         $.ajax({
                             type: "GET",
                             url: "/sellpendingdata",
                             dataType: "json",
                             success: function(response) {
                     
                     
                                
                                 $('tbody.pending').html("");
                                 $.each(response.sellpendingdata, function(key, item) {
                                 
                                         $('tbody.pending').append('<tr class="pend-' + item.id + '">\
                                                         <th>' + item.created_at+'</th>\
                                                         <td class="text-center">BCCBTC</td>\
                                                         <td class=" text-center">BUY</td>\
                                                         <td >' + item.price + '</td>\
                                                         <td >' + item.amount + '</td>\
                                                         <td>' + item.total + '</td>\
                                                         <td>Pending</td>\
                                                         <td><button type="button" value="' + item.id + '" class="delete btn btn-sm btn-outline-primary p-2" style="font-size: 0.4em;">Cls</button></td>\
                                                         </tr>');
                     
                     
                                
                                 });
                     
                             }
                         });
                     }
                     
                 


how can i show only date and time as

2020-06-30 22:35:59
0 likes
3 replies
tykus's avatar
tykus
Best Answer
Level 104

The timestamps will be Carbon instances; you can use the toDateString method to display the Y-m-d H:i:s format. If you override the toArray method on the Model; you can specify how the timestamps should be serialized:

    public function toArray()
    {
        return array_merge(parent::toArray(), [
            'created_at' => $this->created_at->toDateTimeString(),
            'updated_at' => $this->updated_at->toDateTimeString(),
        ]);
    }

Or, you can cast the created_at and updated_at timestamps with defined formats:

    protected $casts = [
        'created_at' => 'datetime:Y-m-d H:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s',
    ];
2 likes
Mareco's avatar

Hi @zwarkyaw,

or you can just format it in controller via collection method map:

return response()->json([
	'sellpendingdata' => $sellpendingdata->map(function($item) {
		return [
			// other parameters
			'created_at' => $item->format('Y-m-d H:i:s')
 		]
	})
]);
simonhamp's avatar

@zwarkyaw as your datetime fields are being serialised to JSON, they're being represented in a portable string format that attempts to maintain as much detail about the timestamp object as possible.

This means you'll either need to lose some information about the object by defining the format you want server-side and then displaying that on the client OR to parse that default string format in Javascript back into a date object that you can then format how you'd like on the frontend.

For example, Date.parse() in your Javascript could help:

let created_at = new Date().setTime(Date.parse(item.created_at));
...
<th>' + created_at.getYear() + created_at.getMonth() + created_at.getDate() +'</th>\

The available methods on the Date class will expose all you need.

See MDN web docs for more info on Date.parse() and the other methods

However, I find this kind of verbose and not super helpful. There are a bunch of wrapper libraries that you could use instead, like Moment or Luxon, that make all of this a little more approachable with nicer interfaces.

Please or to participate in this conversation.