PHPackages                             erag/laravel-datetime-format - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. erag/laravel-datetime-format

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

erag/laravel-datetime-format
============================

Format Eloquent date and datetime values consistently across models, Blade, and APIs.

v1.0.1(1mo ago)83MITPHPPHP ^8.2CI passing

Since May 27Pushed 1mo agoCompare

[ Source](https://github.com/eramitgupta/laravel-datetime-format)[ Packagist](https://packagist.org/packages/erag/laravel-datetime-format)[ GitHub Sponsors](https://github.com/eramitgupta)[ RSS](/packages/erag-laravel-datetime-format/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (2)Dependencies (8)Versions (3)Used By (0)

Laravel DateTime Format
=======================

[](#laravel-datetime-format)

A powerful Laravel package for centralized and consistent date/time formatting across your application. Automatically format Eloquent model dates, API responses, Blade output, and Carbon instances without repeating manual -&gt;format(...) calls everywhere.

Key Features 🔥
--------------

[](#key-features-)

- 👤 Automatic Eloquent model datetime formatting
- 🧠 Centralized datetime formatter service
- 🧩 Custom cast support with `FormattedDateTimeCast`
- 🎨 Blade directive support via `@dateTimeFormat(...)`, `@datetime(...)`, `@dateFormat(...)`, and `@timeFormat(...)`
- ⏱️ Carbon macro support using `toConfiguredFormat()`
- 📦 API resource helper macros
- 🌍 Timezone and locale support
- 🧱 Laravel `10`, `11`, `12`, and `13` support
- ✅ PHP `8.2+` support

Install 🚀
---------

[](#install-)

```
composer require erag/laravel-datetime-format
php artisan erag:install-datetime-format
```

Config ⚙️
---------

[](#config-️)

Published file: `config/datetime-format.php`

```
return [
    'format' => 'd-m-Y H:i:s',
    'timezone' => env('APP_TIMEZONE', 'UTC'),
    'locale' => env('APP_LOCALE', 'en'),
    'null_value' => null,
    'auto_apply' => true,
    'date_format' => 'd-m-Y',
    'time_format' => 'H:i:s',
];
```

### What `timezone` and `locale` do 🌍

[](#what-timezone-and-locale-do-)

- `timezone`: defines the timezone used for formatted output.
    Example: the input can be UTC, but output can be converted to `Asia/Kolkata`.
- `locale`: sets Carbon’s language/context before formatting.
    This is useful when using month/day names, such as `28 May 2026` or localized month labels.

Quick Understanding (Before vs After) 👀
---------------------------------------

[](#quick-understanding-before-vs-after-)

Without package (common output):

```
{
  "created_at": "2026-05-27T15:39:13.000000Z"
}
```

With package + trait:

```
{
  "created_at": "27-05-2026 21:09:13"
}
```

Usage
-----

[](#usage)

### 1) Model Auto Format (Recommended) 👤

[](#1-model-auto-format-recommended-)

```
use LaravelDateTimeFormat\Concerns\HasFormattedDateTimes;

class User extends Model
{
    use HasFormattedDateTimes;
}
```

By default, every formatted date uses the global package config:

- `datetime-format.format`
- `datetime-format.timezone`
- `datetime-format.locale`

Controller:

```
return response()->json([
    'user' => User::first(),
]);
```

Response example:

```
{
  "user": {
    "id": 1,
    "name": "Kaden Herring",
    "email": "biwepa@mailinator.com",
    "created_at": "27-05-2026 21:09:13",
    "updated_at": "27-05-2026 21:09:13"
  }
}
```

### 1.1) Different format per attribute 👤

[](#11-different-format-per-attribute-)

Use `formattedDateAttributes()` when you want different output formats on the same model:

```
use LaravelDateTimeFormat\Concerns\HasFormattedDateTimes;

class User extends Model
{
    use HasFormattedDateTimes;

    protected function formattedDateAttributes(): array
    {
        return [
            'created_at' => 'd-m-Y',
            'updated_at' => 'd/m/Y h:i A',
            'email_verified_at' => 'M d, Y',
        ];
    }
}
```

Example output:

```
{
  "created_at": "29-05-2026",
  "updated_at": "29/05/2026 10:30 AM",
  "email_verified_at": "May 29, 2026"
}
```

### 1.2) Different format and timezone per attribute 🌍

[](#12-different-format-and-timezone-per-attribute-)

Use `$formattedDate` when a field needs additional options like `timezone` or `locale`:

```
use LaravelDateTimeFormat\Concerns\HasFormattedDateTimes;

class User extends Model
{
    use HasFormattedDateTimes;

    protected array $formattedDate = [
        'created_at' => ['format' => 'd-m-Y', 'timezone' => 'UTC'],
        'updated_at' => ['format' => 'd/m/Y h:i A', 'timezone' => 'Asia/Kolkata'],
        'email_verified_at' => ['format' => 'M d, Y', 'locale' => 'en'],
    ];
}
```

This is enough by itself. You do not need to repeat the same fields in `formattedDateAttributes()`.

Example output:

```
{
  "created_at": "29-05-2026",
  "updated_at": "29/05/2026 10:30 AM",
  "email_verified_at": "May 29, 2026"
}
```

### 1.3) Real `User.php` example ✅

[](#13-real-userphp-example-)

```
