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

boldstar's avatar

How To Change Date Format Using Carbon

so I am looking for a way that I can transform the date format when exporting. I am using the maatwebsite/excel package. when importing the dates are changed to the following format dd-mm-yyyy hh:mm:ss which is fine for storing but when exporting I am trying to change the format to d/m/y however what I have tried doesn't seem to do anything. Im pretty sure I am missing something.

Here is the ClientsExport.php file

<?php

namespace App\Exports;

use App\Models\Tenant\Client;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;

class ClientsExport implements FromQuery, WithHeadings
{
    /**
    * @return \Illuminate\Support\Query
    */
    public function query()
    {
        $clients = Client::select(
            'active',
            'category',
            'referral_type', 
            'first_name', 
            'middle_initial', 
            'last_name', 
            'occupation', 
            'dob', 
            'email', 
            'cell_phone', 
            'work_phone',
            'has_spouse',
            'spouse_first_name', 
            'spouse_middle_initial', 
            'spouse_last_name', 
            'spouse_occupation', 
            'spouse_dob', 
            'spouse_email', 
            'spouse_cell_phone', 
            'spouse_work_phone', 
            'street_address',
            'city',
            'state',
            'postal_code',
            'created_at',
            'updated_at'

        );

        foreach($clients as $client) {
            return \Carbon\Carbon::createFromFormat('d/m/y', $client->dob);
        };

        return $clients;
    }

    public function headings(): array
    {
        return [
            'active',
            'category',
            'referral_type', 
            'first_name', 
            'middle_initial', 
            'last_name', 
            'occupation', 
            'dob', 
            'email', 
            'cell_phone', 
            'work_phone',
            'has_spouse',
            'spouse_first_name', 
            'spouse_middle_initial', 
            'spouse_last_name', 
            'spouse_occupation', 
            'spouse_dob', 
            'spouse_email', 
            'spouse_cell_phone', 
            'spouse_work_phone', 
            'street_address',
            'city',
            'state',
            'postal_code',
            'created_at',
            'updated_at'
        ];
    }
}

notice the

foreach($clients as $client) {
            return \Carbon\Carbon::createFromFormat('d/m/y', $client->dob);
        };
0 likes
8 replies
s4muel's avatar

Do not return the value in the iteration, try to replace the $client->dob instead

boldstar's avatar

@S4MUEL - Thanks for the reply! I am not exactly sure what you mean but I assumed you meant something like this?

foreach($clients as $client) {
 \Carbon\Carbon::createFromFormat('d/m/y', $client->dob);
};
itsricky's avatar

Hey @boldstar , I think what @s4muel is getting at is that you're creating a carbon instance inside of a foreach, but it's not really going anywhere. It's just happening in the background and the value is being lost.

I think s4muel means you should reassign the client's dob property once it's been turned to an array.

foreach($clients as $client) {
   $client->dob = Carbon::createFromFormat('d/m/y', $client->dob);
}
1 like
boldstar's avatar

@ITSRICKY - thank you for the reply! yeah so I gave that a shot as well and it returns the original format of dd-mm-yy hh:mm:ss

which is also what I got by trying to return the value as well in the foreach Ill keep playing around with it!

Cronix's avatar

If your dob date is stored as a normal date in the actual db (yyyy-mm-dd), then your carbon code is not right.

Carbon::createFromFormat('d/m/y', $client->dob);

That is saying "the date in the database is already in d/m/y format, make a regular date out of it", which won't work if the format in the db is actually yyyy-mm-dd.

Carbon::parse($client->db)->format('d/m/y');

is probably what you're wanting. (parse the yyyy-mm-dd date in the database as a date, and convert it to d/m/y format).

1 like
boldstar's avatar

@CRONIX - sorry for the late response!

so I am still having issues. I changed my method to just pull one record with the parse method and it returns an error. Now prior to trying to parse the dob I made sure that I was at least returning the record return $dob; and it just obviously returns the database format of yyyy-mm-dd So here is the method currently with trying to parse to format d/m/y

$dob = Client::select('dob')->where('id', 9);
       
$formatedDOB = \Carbon\Carbon::parse($dob)->format('d/m/y');
         
return $formatedDOB;
Cronix's avatar
Cronix
Best Answer
Level 67

You still need to access the dob property on the $dob variable in your carbon::parse(). $dob is an object, so that would need to be $dob->dob to get the actual dob property. You also don't execute the query with first(). Do a dd($dob), what is it? A query builder instance, not your date.

$id = 9;
$client = Client::select('dob')->firstOrFail($id);

return \Carbon\Carbon::parse($client->dob)->format('d/m/y');

Please or to participate in this conversation.