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

GodziLaravel's avatar

How to declare this date format in migration

Hello ,

I would like to create a column (date) with this specific date format :

"updated_at" => "2019-08-03T10:05:01+00:00"

how to do that in the migration ?

0 likes
3 replies
mstrauss's avatar

Hi @mostafalaravel

From the docs: https://laravel.com/docs/5.8/eloquent-mutators#date-mutators

Date Formats

By default, timestamps are formatted as 'Y-m-d H:i:s'. If you need to customize the timestamp format, set the $dateFormat property on your model. This property determines how date attributes are stored in the database, as well as their format when the model is serialized to an array or JSON:

So try something like:

Model with the unique date

class YourModel extends Model
{
    /**
     * The storage format of the model's date columns.
     *
     * @var string
     */
    protected $dateFormat = "Y-m-d\TH:i:s\+u'';
}

I wasn't really sure what you were going for with the 00:00, I assumed milliseconds. But be careful with the precision amount and potentially an improper date format (where Carbon might blow up). If there are issues, try storing the dates as a string instead of a date time. Or, and perhaps this is the easier solution depending on your use-case, just store the updated_at field the standard way Y-m-d H:i:s and the use an accessor mutator when pulling the date from the DB:

Model with the unique date

class YourModel extends Model
{
    public function getUpdatedAtAttribute($value)
    {
        $this->attributes['updated_at'] = $value->format('Y-m-d\TH:i:s\+u');
    }
}

Please or to participate in this conversation.