nette / utils

🛠 Lightweight utilities for string & array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.
https://doc.nette.org/utils
Other
1.98k stars 147 forks source link

Add getIntlDateFormat method in Datetime class #280

Closed petrparolek closed 2 years ago

petrparolek commented 2 years ago

In Nette Utils I didn't find method which I could replace deprecated PHP function strftime('%B', $datetime->getTimestamp()), which I used to Czech translation of munth.

I found solution at https://stackoverflow.com/a/46275845/6346869

petrparolek commented 2 years ago

here are formats https://unicode-org.github.io/icu/userguide/format_parse/datetime/#date-field-symbol-table

stepapo commented 2 years ago

For me this would be useful, but why is it static and not like return $formatter->format($this);?

JanTvrdik commented 2 years ago

Instead of

echo Nette\Utils\DateTime::getIntlDateFormat(
    format: 'LLLL YYYY HH:mm:ss',
    time: new DateTimeImmutable('2021-02-01 15:47:21'),
    locale: 'cs_CZ',
    timeZone: 'Europe/Prague'
)

you can just use

echo IntlDateFormatter::formatObject(
    datetime: new DateTimeImmutable('2021-02-01 15:47:21', new DateTimeZone('Europe/Prague')),
    format: 'LLLL YYYY HH:mm:ss',
    locale: 'cs_CZ',
);
petrparolek commented 2 years ago

For me this would be useful, but why is it static and not like return $formatter->format($this);?

thanks

dg commented 2 years ago

I think it's better to use IntlDateFormatter::formatObject

petrparolek commented 2 years ago

thanks @dg and @JanTvrdik for solution