vincenzoraco / date-formatter-php
A package to generate an array with different date formats
v1.0.0
2025-03-02 10:16 UTC
Requires
- php: ^8.3.0
- nesbot/carbon: ^3.8
Requires (Dev)
- laravel/pint: ^1.18.1
- pestphp/pest: ^3.5.1
- pestphp/pest-plugin-type-coverage: ^3.1
- phpstan/phpstan: ^1.12.7
- rector/rector: ^1.2.8
- symfony/var-dumper: ^7.1.6
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 theDateFormatterInterface
.
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.