thephpleague / period

PHP's time range API

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Possible to determine the end of a duration in a sequence?

jeremyswright opened this issue · comments

Q A
Version 4.11.0

Question

Is there a way for me to calculate the end of a duration applied against a sequence? Better still, calculate the end of a duration from a datepoint in a sequence?

For example:

$sequence = new Sequence(
    new Period('2020-10-30 16:39:12', '2020-10-30 17:00:00'),
    new Period('2020-11-02 09:00:00', '2020-11-02 17:00:00'),
    new Period('2020-11-03 09:00:00', '2020-11-03 17:00:00')
);

$duration = Duration::create(new DateInterval('PT40M'));

echo $sequence->magic($duration) // datepoint 2020-11-02 09:20:48

echo $sequence->betterMagicBit(
    Datepoint::create(new DateTimeImmutable('2020-11-02 16:39:12')),
    $duration
);  // returns datepoint('2020-11-03 09:20:12')

echo $sequence->betterMagicBit(
    Datepoint::create(new DateTimeImmutable('2020-11-02 18:30:00')),
    $duration
);  // returns datepoint('2020-11-03 09:40:00')

I could iterate through the periods in the sequence, subtracting the duration until it's exhausted, but having used the library a few times I can't help think that there's a simpler way that's eluding me.

Thanks.

@jeremyswright unless I misunderstood your question. I think what you are looking for is found here https://period.thephpleague.com/4.0/sequence/container/#sequence-information

Thanks. I'm familiar with that page, and have used many of the methods in it, but I couldn't figure out how/which to apply to get the result I outlined above.

So I've written my own calculator now, using the iterative method I eluded to at the bottom of my message.