Skip to content

ivandokov/fluentdate

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
src
 
 
 
 
 
 
 
 
 
 
 
 

FluentDate

Tests Action Status Packagist PHP Version Support Packagist Version Packagist Downloads Packagist License

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

About

Library to format PHP dates

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages