/date-formatter

A custom Laravel package for formatting dates.

Primary LanguagePHPMIT LicenseMIT

Installation

You can install the package via Composer:

composer require shafayat/date-formatter

Configuration

To customize the package's default settings, publish the configuration file:

php artisan vendor:publish --tag=config

This will create a config/dateformatter.php file where you can define your date formats:

return [
    'default_format' => 'Y-m-d',
    'styles' => [
        'short' => 'm/d/Y',
        'long' => 'F d, Y',
        'custom' => 'd-m-Y H:i:s',
    ],
];

Usage

Formatting a Date

You can format dates using the format method:

use Shafayat\DateFormatter\DateFormatter;

// Format a string date
echo DateFormatter::format('2024-01-01', 'short'); // Output: 01/01/2024

// Format a DateTime instance
$date = new \DateTime('2024-01-01');
echo DateFormatter::format($date, 'long'); // Output: January 01, 2024

Default Format

If no style is specified, the default_format from the configuration will be used:

echo DateFormatter::format('2024-01-01'); // Output: 2024-01-01

Testing

This package comes with unit tests. To run them, navigate to the package directory and execute:

vendor/bin/phpunit

Roadmap

  • Add support for localization.
  • Introduce customizable input date parsing.
  • Add timezone-aware date formatting.

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository.
  2. Create a feature branch: git checkout -b feature-name.
  3. Commit your changes: git commit -m 'Add feature name'.
  4. Push to the branch: git push origin feature-name.
  5. Create a pull request.

License

This package is open-source software licensed under the MIT License.