thephpleague / period

PHP's time range API

Home Page:https://period.thephpleague.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can the lib handle periods without/ignoring year?

c33s opened this issue · comments

Q A
Version 3.4 / 4.10

Question

for handling birthdays or different opening times in holidays i look for checking if a given date is inside a specific period. the year does not matter because it's the same period every year.

$holidayOpeningTimes = new Period(
    new DateTime('2014-06-01 00:00:00'),
    new DateTimeImmutable('2014-07-01 00:00:00'),
    Period::INCLUDE_START_EXCLUDE_END,
    Period::INGNORE_YEAR
);

$today = new DateTime() //2020-06-22

$holidayOpeningTimes->contains($today) // returns true or false in this case  -> true

do i miss something or is this not possible with period. just had a look in the docs...

related links:

https://stackoverflow.com/questions/37607512/compare-only-day-and-month-without-year-in-php-ex-birth-date?rq=1
https://stackoverflow.com/questions/35236103/php-compare-dates-without-year

@c33s you can always use Period::move if needed. Just look at the documentation website

@nyamsprod but wouldn't it be a good feature (better DX - developer experience) to have this feature directly integrated?

having the following period:

$holidayOpeningTimes = new Period(
    new DateTimeImmutable('2014-06-01 00:00:00'),
    new DateTimeImmutable('2014-07-01 00:00:00'),
);
$today = Datepoint::create(new DateTime());

working with it:

$holidayOpeningTimes->containsDateIgnoreYear($today) //true

vs

$currentYear = date('Y'); //2020
$startYear = $holidayOpeningTimes->getStartDate()->format('Y'); //2014
$offset = $currentYear - $startYear //2020 - 2014 = 6
$currentYearOpeningTimes = $holidayOpeningTimes->move("$offset YEAR"); //2020-06-01 to 2020-07-01
$today->isDuring($currentOpeningTimes);

shouldn't this lib provide a broader api to help developer that they don't have do manual calculations or do i again miss something in the docs?

also i miss some kind of "precision" like in https://github.com/spatie/period#precision

@c33s Period is a low level package which aims at resolving issue with date and time intervals. What you seems to be looking for is a library dedicated to recurring events which aim at solving a very different scenario. That's why you are require to apply business logic on top of the functionalities provided by the package in order to solve your issue.

As for the precision feature this is out of scope for this specific issue. If you want to discuss its inclusion in the current package please create a separate ticket for that. This helps me and other track changes in the package.