vincenzoraco/date-formatter-php 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

vincenzoraco/date-formatter-php

最新稳定版本:v1.0.0

Composer 安装命令:

composer require vincenzoraco/date-formatter-php

包简介

A package to generate an array with different date formats

README 文档

README

This package provides a utility to generate an array with different date formats

Requires PHP 8.3+

⚡️ Install the package using Composer:

composer require vincenzoraco/date-formatter-php

Features

  • Easily format dates in various formats.
  • Out-of-the-box formatters available (see the /src/Formatters folder).
  • Option to create and use custom formatters by extending DateFormatterAbstract or implementing the DateFormatterInterface.

Usage

The FormatDate class accepts a DateTime|DateTimeImmutable|Carbon|CarbonImmutable object and allows you to add multiple formatters. Here's an example of how to use it:

(new FormatDate(CarbonImmutable::now()))
  ->add(new Rfc822Formatter)
  ->add(new Rfc850Formatter)
  ->add(new Iso8601Formatter)
  ->add((new TimestampFormatter)->setKey("unix"))
  ->toArray();

// Result
[
    "rfc_822" => "Sun, 02 Mar 25 09:25:01 +0000",
    "rfc_850" => "Sunday, 02-Mar-25 09:25:01 UTC",
    "iso_8601" => "2025-03-02T09:25:01+00:00",
    "unix" => "1740907501",
]

Custom Formatters

To add a custom formatter, you can either extend the abstract class DateFormatterAbstract, which includes several helper methods, or implement the DateFormatterInterface directly.

Example of a custom formatter:

use VincenzoRaco\DateFormatterAbstract;

class CustomFormatter extends DateFormatterAbstract
{
    protected string $key = 'custom_key';

    public function __toString(): string
    {
        return $this->getDate()->format('d/m/Y');
    }
}

License

This package was created by Vincenzo Raco and is licensed under the MIT License.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-03-02