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.
composer require ivandokov/fluentdateFor full details of how to use the library please take a look at our tests.
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$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.09The $date can be directly used in strings since it implements __toString() magic method but if you want you can use the ->toString() method.
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.dIf 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.09The format classes are self explanatory by their class names so you can take a look at all available classes in the Formats directory.
We support the most common separators for dates but also a custom string separator in case of need.
IvanDokov\FluentDate\Formats\SeparatorDotIvanDokov\FluentDate\Formats\SeparatorDashIvanDokov\FluentDate\Formats\SeparatorForwardSlashIvanDokov\FluentDate\Formats\SeparatorSpaceIvanDokov\FluentDate\Formats\SeparatorColonIvanDokov\FluentDate\Formats\SeparatorString