承接 emilienkopp/formats-dates-trait 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

emilienkopp/formats-dates-trait

Composer 安装命令:

composer require emilienkopp/formats-dates-trait

包简介

A Laravel-Carbon trait for easy date formatting

README 文档

README

Latest Version on Packagist Total Downloads License

A Laravel trait that provides elegant date formatting capabilities to your models with automatic format detection and magic method support.

Features

  • 🔄 Automatic format detection based on property names and types
  • ✨ Magic method support for cleaner syntax
  • 🔍 Reflection-based property access
  • 🛡️ Null-safe operations
  • 🎨 Customizable date formats
  • 🎯 Type-hint aware

Installation

You can install the package via composer:

composer require emilienkopp/formats-dates-trait

Usage

Add the trait to your model:

use EmilienKopp\DatesFormatter\FormatsDates;

class User extends Model
{
    use FormatsDates;

    protected $dates = [
        'created_at',
        'updated_at',
        'birth_date',
        'login_time'
    ];
}

Now you can use 'getFormattedDate' method or magic methods to format your dates.

⚠️ Note: magic methods are camelCase versions of the property names.

Automatic Format Detection

The trait automatically determines the appropriate format based on property names and types. It uses strict suffix matching to avoid false positives.

Format Detection Rules (in order of precedence)

  1. Type hints

    • Properties typed as DateTime → Uses datetime format
  2. Property name suffixes

    • Ends with _datetime or _at → Uses datetime format
    • Ends with _time → Uses time format
    • Ends with _date → Uses date format
    • All others → Uses default date format

Magic methods are generated based on the suffixes. Therefore, any property that does not follow the suffix rules will not have a magic method invoked and will likely throw a BadMethodCallException.

Examples

// DateTime format (Y-m-d H:i:s)
created_at          ✓
updated_at          ✓
deleted_datetime    ✓
attestation          (no underscored suffix)
updater_name         (not a supported suffix)

// Time format (H:i:s)
login_time          ✓
start_time          ✓
datetime_time       ✓
overtime             (no underscore prefix)

// Date format (Y-m-d)
birth_date          ✓
publish_date        ✓
mandated_by          (not a supported suffix)
validate             (no underscore prefix)

Basic Usage

// Using magic methods
$user->createdAt();        // Returns '2024-03-13'
$user->updatedAt();        // Returns '2024-03-13'
$user->loginTime();        // Returns '14:30:00'

// Using custom formats
$user->createdAt('d/m/Y'); // Returns '13/03/2024'
$user->loginTime('H:i');   // Returns '14:30'

// Using the direct method
$user->getFormattedDate('created_at');           // Uses auto-detected format
$user->getFormattedDate('created_at', 'd/m/Y');  // Uses custom format

Customizing Default Formats

You can customize the default formats for your model:

// Customize date format
User::setDefaultDateFormat('d/m/Y');

// Customize datetime format
User::setDefaultDatetimeFormat('d/m/Y H:i');

// Customize time format
User::setDefaultTimeFormat('H:i');

// Get current formats
$dateFormat = User::getDefaultDateFormat();
$datetimeFormat = User::getDefaultDatetimeFormat();

⚠️ Note: Format changes are applied per model class, not globally.

Null Safety

The trait handles null values gracefully:

$user->birth_date = null;
$user->birthDate(); // Returns null

License

The MIT License (MIT). Please see License File for more information.

Changelog

Please see CHANGELOG for more information on what has changed recently.

Security

If you discover any security related issues, please contact me instead of using the issue tracker.

Support Laravel Versions

  • Laravel 7.x through 11.x
  • PHP 7.4 or higher

emilienkopp/formats-dates-trait 适用场景与选型建议

emilienkopp/formats-dates-trait 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 154 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 11 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 emilienkopp/formats-dates-trait 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 emilienkopp/formats-dates-trait 我们能提供哪些服务?
定制开发 / 二次开发

基于 emilienkopp/formats-dates-trait 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 154
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 7
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-13