org_heigl / daterange
Provides a way to display dateranges
Requires
- php: >=5.5
Requires (Dev)
- codeclimate/php-test-reporter: dev-master
- mockery/mockery: ~0.9
- phpunit/phpunit: ~4.7@dev
- squizlabs/php_codesniffer: ~2.0@dev
- dev-master
- 1.2.0
- 1.1.0
- 1.0.0
- dev-dependabot/composer/squizlabs/php_codesniffer-approx-2.0devor-approx-3.0
- dev-dependabot/add-v2-config-file
- dev-dependabot/composer/mockery/mockery-approx-0.9or-approx-0.0
- dev-dependabot/composer/squizlabs/php_codesniffer-approx-2.0@devor-approx-3.0
- dev-dependabot/composer/phpunit/phpunit-approx-4.7@devor-approx-8.0
- dev-feature/addFilter
- dev-feature/removeSeparator
This package is auto-updated.
Last update: 2024-11-29 04:30:27 UTC
README
This small library tries to ease printing of date-ranges.
Installation
Installation is easy via composer. Simply type this in your terminal to add the
DateRange-Library to your composer.conf
-file:
composer require org_heigl/daterange
Usage
You can then use the DateRange library by creating a DateRange-instance,
setting a format and a separator and then simply calling getDateRange()
with the start-date and the end date as parameters.
Simple example:
<?php use Org_Heigl\DateRange\DateRangeFormatter $dateRange = new DateRangeFormatter(); $dateRange->setFormat('d.m.Y'); $dateRange->setSeparator(' - '); echo $dateRange->getDateRange(new \DateTime('12.3.2015'), new \DateTime('13.4.2015')); // Will print: 12.03. - 13.04.2014 echo $dateRange->getDateRange(new \DateTime('12.3.2015'), new \DateTime('13.3.2015')); // Will print: 12. - 13.03.2014
More complex example:
<?php use Org_Heigl\DateRange\DateRangeFormatter $dateRange = new DateRangeFormatter(); $dateRange->setFormat('m/d/Y'); $dateRange->setSeparator(' - '); echo $dateRange->getDateRange(new \DateTime('12.3.2015'), new \DateTime('13.3.2015')); // Will print: 3/12/ - 3/13/2014
You want to change parts of the date-formatting string? Try the Filters.
If you want to display something like 12 - 13.03.2013 (node the missing dot after the 12)
you can use the formatting-string d.m.Y
and add a RemoveEverythingAfterLastDateStringFilter
lik this:
<?php use Org_Heigl\DateRange\DateRangeFormatter; use Org_Heigl\DateRange\Filter\RemoveEverythingAfterLastDateStringFilter; $dateRange = new DateRangeFormatter(); $dateRange->setFormat('d.m.Y'); $dateRange->setSeparator(' - '); $dateRange->addFilter(new RemoveEverythingAfterLastDateStringFilter(), DateRangeFormatter::FILTER_FIRST_DIFF); echo $dateRange->getDateRange(new \DateTime('12.3.2015'), new \DateTime('13.3.2015')); // Will print: 12 - 13.03.2014
Currently the following Filters are available:
- RemoveEverythingAfterLastDateStringFilter - This filter will remove everything
after the last dateformatting-character in the given date-part. So when the
dateformatting-string reads
d.m.
it will remove everything behind them
which is the last dateformatting-character. - TrimFilter - This filter will remove excess whitespace. It just passes the dateformatting-string through the ```trim``-function.
You can implement your own filter by simply implementing the Org_Heigl\DateRange\DateRangeFilterInterface
That way evertything is possible!
You can add a filter to four different filterchains that filter different parts of the formatting string.
- DateRangeFormatter::FILTER_COMPLETE will be applied to a formatting string
when first and second day are the same. So the input will be the formatting-string
you provided via the
DateRangeFormatter::setFormat()
. - DateRangeFormatter::FILTER_FIRST_DIFF will be applied to the first part of
the splitted formatting string that is used for the start-date. So when your
formatting string is
d.m.Y
and the dates differ in the month the filter will be applied tod.m.
for the starting date - DateRangeFormatter::FILTER_SECOND_DIFF will be applied to the first part of
the splitted formatting string that is used for the end-date. So when your
formatting-string is
d.m.Y
and the dates differ in the month the filter will be applied tod.m.
for the end-date - DateRangeFormatter::FILTER_SAME will be applied to the second part of the
splitted formatting string that is used for the part that is equal on start-
and end-date. So when your formatting-string is
d.m.Y
and the dates differ in the day the filter will be applied tom.Y
.