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

charlesmun's avatar

Format Laravel Timestamp using Carbon

I am trying to format all timestamps from the database but am not getting required result. I am using the map function. I also tried with the foreach but nothing working. Am implementing this on the controller and sending the result to my view.

$invoices = Invoice::where('user_id', $request->id)
		->get()
		->map($invoices, function(){
          return Carbon::createFromFormat('Y-m-d H:i:s', $invoices->created_at)->format('Y-m-d');		
		});
0 likes
9 replies
Sinnbeck's avatar

Created at is already a carbon instance. Also you need to actually change the invoice and return it

$invoices = Invoice::where('user_id', $request->id)
		->get()
		->map($invoices, function($invoice){
         $invoice->created_at = $invoice->created_at->toDateString();
          return $invoice;
		});

But you could just do this in blade directly ?

charlesmun's avatar

@Sinnbeck Thank you for the quick response. Makes perfect sense that the date is already a carbon instance.

This is the return I am getting

2022-12-15t00:00:00.000000z

After formatting the date.

charlesmun's avatar

@Sinnbeck Here is my full implementation

public function fetchInvoices(Request $request)
    {
        if( $request->ajax() ){

            $invoices = Invoices::where('user_id', $request->id)
            ->get()
            ->map($invoices, function($invoice){
                $invoice->created_at = $invoice->created_at->toDateString();
                return $invoice;
            });

        $totalInvoices= 0.00;
            foreach($invoices as $value){
              $invoicesTotal += $value->amount;
            }

        return response()->json([
            'user_invoices'       => $invoices,
            'invoices_total'      => $totalInvoices,
        ], 200);

        }
    }

And then, in my blade file I am outputting

response.user_invoices.created_at

Since am using javascript on the frontend

My output on the screen however is

2022-12-15t00:00:00.000000z

And I want it to be more readable like

Invoice Created on 15-12-2022 ... and the time...

newbie360's avatar
Level 24

@charlesmun add an Accessor for that created_at, may be call that created_date

    protected function createdDate(): Attribute
    {
        return Attribute::make(
            get: function ($value, $attributes) {
                return \Carbon\Carbon::parse($attributes['created_at'])->format('Y-m-d');
            }
        );
    }

and calc the $invoicesTotal just use sum()

        return response()->json([
            'user_invoices'       => $invoices,
            'invoices_total'      => $invoices->sum('amount'),
        ], 200);

can't see response of this variable $invoicesTotal

1 like
charlesmun's avatar

@newbie360 Thanks for the assistance. I ended up creating an Accessor, the code i used is below

protected function createdAt(): Attribute
    {
        return new Attribute(
            get: fn ($value) => Carbon::parse($value)->format('D d M Y | h:mA'),
        );
    }

To anyone with the same issue, you place this code in your Model and ensure that you have the below classes included in your Model

use Illuminate\Database\Eloquent\Casts\Attribute;
use Carbon\Carbon;

More information regarding this is on the documentation https://laravel.com/docs/9.x/eloquent-mutators

1 like

Please or to participate in this conversation.