ivandokov / fluentdate
Library to format PHP dates
Requires
- php: ^7.4.0
Requires (Dev)
- nesbot/carbon: ^2.41
- pestphp/pest: ^0.3.8
This package is auto-updated.
Last update: 2024-04-13 17:41:23 UTC
README
Format PHP dates with ease. You don't have to Google the format string each time. Use plain English to set the format, almost as you pronounce it.
Installation
composer require ivandokov/fluentdate
How to use
For full details of how to use the library please take a look at our tests.
Format string
Any string that can be parsed by DateTime
class can be used.
$date = FluentDate::forString('now') ->year(new YearFourDigits()) ->separator(new SeparatorDot()) ->month(new MonthTwoDigitsWithLeadingZeros()) ->separator(new SeparatorDot()) ->day(new DayWithLeadingZero()); echo "Today is $date"; // Today is 2020.10.09
Format DateTime
$datetime = new DateTime('now'); // The DateTime you want to format $date = FluentDate::forDateTime($datetime) ->year(new YearFourDigits()) ->separator(new SeparatorDot()) ->month(new MonthTwoDigitsWithLeadingZeros()) ->separator(new SeparatorDot()) ->day(new DayWithLeadingZero()); echo "Today is $date"; // Today is 2020.10.09
The $date
can be directly used in strings since it implements __toString()
magic method but if you want you can use the ->toString()
method.
The format string
If you want to get the underlying date format string you can use the ->toFormat()
method like this:
$datetime = new DateTime('01.03.2020 00:00:00'); $dateFormat = FluentDate::forDateTime($datetime) ->year(new YearFourDigits()) ->separator(new SeparatorDot()) ->month(new MonthTwoDigitsWithLeadingZeros()) ->separator(new SeparatorDot()) ->day(new DayWithLeadingZero()) ->toFormat(); // Y.m.d
Format Carbon
If you want to format a Carbon instance you can use the same method described above since Carbon is extending DateTime
.
$carbon = Carbon::now(); $date = FluentDate::forDateTime($carbon) ->year(new YearFourDigits()) ->separator(new SeparatorDot()) ->month(new MonthTwoDigitsWithLeadingZeros()) ->separator(new SeparatorDot()) ->day(new DayWithLeadingZero()) ->toString(); // 2020.10.09
Available formats
The format classes are self explanatory by their class names so you can take a look at all available classes in the Formats directory.
Separators
We support the most common separators for dates but also a custom string separator in case of need.
IvanDokov\FluentDate\Formats\SeparatorDot
IvanDokov\FluentDate\Formats\SeparatorDash
IvanDokov\FluentDate\Formats\SeparatorForwardSlash
IvanDokov\FluentDate\Formats\SeparatorSpace
IvanDokov\FluentDate\Formats\SeparatorColon
IvanDokov\FluentDate\Formats\SeparatorString