flack / ranger

Formatter for date and time ranges with i18n support
73 stars 15 forks source link

Support for all day events #11

Open iantearle opened 1 year ago

iantearle commented 1 year ago

Would it be possible to support dates where the times are set to 00:00 to 23:59 - this seems to be a popular setting to display an all day event.

It would be nice to have the option to pass in a string to read "all day"

flack commented 1 year ago

Hi!

I'm not exactly sure what it is you want to show, could you give a more concrete example?

The thing about showing a text like "all day" is also that that would need localization, so we'd need to add some translation framework (but well, if it's only one string I suppose it could be added to the Provider classes)

iantearle commented 1 year ago

Sure, Sorry; Given the values: Start date: 1 April 2023 00:00 End date: 1 April 2023 23:59

Currently the event would appear as "April, 1, 2023 - 00:00 - 23:59"

echo $ranger->format('2023-04-01 00:00', '2023-04-01 23:59');
// April 1, 2023, between 00:00 AM and 11:59 PM

It would be nice to have it read as "April, 1, 2023 - All day"

// April 1, 2023, All day
flack commented 1 year ago

Ok, thx, I think I get it now. What would

echo $ranger->format('2023-04-01 00:00', '2023-04-02 23:59');

look like? Would that be Apr 1, 2023, 12:00 AM – Apr 2, 2023, 11:59 PM or Apr 1–2, 2023 - All day?

Also, what happens if you use

$ranger->setTimeType(IntlDateFormatter::NONE)

(which is the default setting), does this still show the All Day, or would it just show the date(s)?

iantearle commented 1 year ago

I think if the event is over multiple days but the timing is 00:00-23:59 it should still say all day, but if that timing deviates it should stipulate the time.

Setting NONE would also hide the All day string.

Incidentally, I don't think it's possible, correct me if I'm wrong, but this may need an additional ticket, but I couldn't work out how to pass a pattern() to IntlDateFormatter?

flack commented 1 year ago

ok, so roughly speaking, the code would look something like this:

function allday($start, $end)
{
    $ranger = new Ranger('en_US');
    $ranger->setTimeType(IntlDateFormatter::MEDIUM);
    $suffix = '';
    if (str_ends_with($start, ' 00:00') && str_ends_with($end, ' 23:59')) {
        $ranger->setTimeType(IntlDateFormatter::NONE);
        $start = explode(' ', $start)[0];
        $end = explode(' ', $end)[0];
        $suffix = ', All day';
    }
    return $ranger->format($start, $end) . $suffix;
}

echo allday('2023-04-01 00:00', '2023-04-02 23:59');

right?

ofc this example code only works with exactly one input format and doesn't account for time format settings. I wonder how to best go about integrating that. Maybe I should change some functions from private to protected, then this could be easily implemented in a Ranger subclass. Another option might by to add FormatPreprocessor / FormatPostprocessor interfaces/hooks or similar so that users cann supply ther own custom logic, not sure how messy that would be..

For the pattern, yes, that is currently not supported. I remember vaguely looking into it at some point and it seemed complicated to do, but I don't remember why exactly. Skimming the code currently it actually looks relatively simple (but I'm probably missing something). In any case, that would be a separate ticket.