You can install the package via Composer:
composer require shafayat/date-formatter
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',
],
];
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
If no style is specified, the default_format
from the configuration will be used:
echo DateFormatter::format('2024-01-01'); // Output: 2024-01-01
This package comes with unit tests. To run them, navigate to the package directory and execute:
vendor/bin/phpunit
- Add support for localization.
- Introduce customizable input date parsing.
- Add timezone-aware date formatting.
Contributions are welcome! Please follow these steps:
- Fork the repository.
- Create a feature branch:
git checkout -b feature-name
. - Commit your changes:
git commit -m 'Add feature name'
. - Push to the branch:
git push origin feature-name
. - Create a pull request.
This package is open-source software licensed under the MIT License.