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

divinulledivi's avatar

Date cast formatting not working

As the title says - date casting and date formatting is not working as expected. How I'm storing dates:

  1. Create a date with carbon:
$date = Carbon::parse('Tue, 04 Feb 2020 02:00:00 +0200');

// result:
// Illuminate\Support\Carbon @1580774400 {#702
//    date: 2020-02-04 02:00:00.0 +02:00
// }

  1. Save in database date field which is defined as datetime:
$table->datetime('date');
  1. Cast it as d.m.Y in model:
protected $casts = [
        'date' => 'datetime:d.m.Y H:i:s',
    ];

The result I'm getting: 2020-02-07T00:00:00.000000Z

Am I doing something wrong? (This is on Laravel 6.2)

0 likes
3 replies
jlrdw's avatar
$tdate = new \DateTime($yourdate);
$ndate = $tdate->format('Y-m-d');

Change the format to what you need.

a4ashraf's avatar

@memele

Try this one


    $your_model = Your Model::find(1);

    $arrYourModel = $your_model->toArray();

    echo $arrYourModel['date'];


it working perfectly at my end

otherwise you can create an accessor method in your model as well

  public function getDateAttribute() {
    return date('d.m.Y H:i:s', strtotime($this->attributes['date']));
  }

Please or to participate in this conversation.