How to change default format at created_at and updated_at value laravel

How to change default format at created_at and updated_at value laravel

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Changing Timestamp Formats in Laravel Models

When working with Laravel's timestamps, the default behavior for created_at and updated_at columns is to store both date and time information in a standard format like 2014-06-26 04:07:31. This works perfectly for database-level operations where millisecond precision matters. However, there are scenarios where you need to display only the date portion, such as 2014-06-26, on your frontend views or reports.

It is important to understand that Laravel stores these timestamps in a normalized way within the database (typically datetime type) regardless of how they appear when displayed. The solution isn't to alter the raw data stored in the database, but rather to adjust how that data is formatted during display or when filling the model attributes.

Using Carbon Traits for Formatting

The most robust approach to handle timestamp formatting in Laravel is leveraging the Carbon library, which is included as a trait within the framework's model classes. By default, Laravel models use the App\Providers\EventServiceProvider or similar bootstrappers to include these traits, but you can also explicitly enable them in your specific model if needed.

To change the format without altering the underlying database value, you should define a custom accessor method in your Model class. This allows you to keep the standard created_at and updated_at columns in the database while presenting the data differently when accessed via $post->created_at.

Here is how you can implement this in your Post model:

class Post extends \Eloquent {
    use \Illuminate\Database\Eloquent\SoftDeletes; // Or any other traits you need

    protected $fillable = [
        'title', 'body', 'meta', 'reporter', 'slug', 'image', 'top'
    ];

    // Define a custom format for the created_at column
    public function getCreatedAtAttribute($value)
    {
        if (empty($value)) {
            return null;
        }

        $date = \Carbon\Carbon::parse($value);
        return $date->format('Y-m-d');
    }

    // Define a custom format for the updated_at column
    public function getUpdatedAtAttribute($value)
    {
        if (empty($value)) {
            return null;
        }

        $date = \Carbon\Carbon::parse($value);
        return $date->format('Y-m-d');
    }
}

By implementing these accessors, any time you retrieve the $post model instance and call $post->created_at, Laravel will automatically apply the getCreatedAtAttribute method. The result will be a string formatted as Y-m-d, effectively hiding the time component from your application logic and views.

Why Not Change the Database Schema?

A common misconception is that you need to change the column type in your database migration or alter the schema to remove the time component. This is generally not recommended. The datetime type provides a consistent standard for storing temporal data, ensuring that records created at 23:59:59 on one day are distinct from those created early the next morning, even if they share the same date.

Removing the time component from the storage layer can lead to data integrity issues, especially when dealing with time-based queries, sorting, or auditing logs. Instead of changing how the data is stored, change how it is presented. This separation of concerns keeps your database clean and your presentation layer flexible.

Handling Raw Input in Controllers

In your controller's store method, you are manually assigning attributes to the model before saving. While this is valid, be aware that if you were to manually set $post->created_at to a date string without the time, Laravel might overwrite it upon save() because of the timestamp traits automatically injecting values for these columns.

To ensure the formatted dates appear correctly after a save operation (for instance, if you are creating a record with specific manual timestamps), you must assign the values as Carbon instances or ensure the accessors handle the raw database value correctly. In your provided controller code, since you are not manually setting $post->created_at or $post->updated_at, Laravel will automatically generate these values at the moment of save().

Therefore, the accessor methods defined in the model will intercept any read operation immediately after the save, ensuring that whenever your view renders the list of posts, it displays:

2014-06-26

Instead of:

2014-06-26 04:07:31

This approach is cleaner than manipulating the raw input in the controller or modifying the database schema, as it encapsulates the formatting logic within the model itself.

Our network