Manaus Posted January 7, 2023 Posted January 7, 2023 I'm using a Datetime field for displaying event dates, and I get an English format: Sunday, 8 January 2023 10:00 I put the set_locale function in the page, but no: <?php setlocale(LC_TIME, 'it_IT'); ?> How do I get a datetime in italian format? Thanks a lot
Jan Romero Posted January 8, 2023 Posted January 8, 2023 The new thing in PHP for this is the IntlDateFormatter class: $giornoDellaSettimanaFormatter = new \IntlDateFormatter('it_IT', \IntlDateFormatter::FULL, \IntlDateFormatter::FULL, null, null, 'eeee'); echo $giornoDellaSettimanaFormatter->format($page->getUnformatted('data')); //Domenica https://www.php.net/manual/en/class.intldateformatter.php 2
Stefanowitsch Posted January 8, 2023 Posted January 8, 2023 22 hours ago, Manaus said: I'm using a Datetime field for displaying event dates, and I get an English format: Sunday, 8 January 2023 10:00 I put the set_locale function in the page, but no: <?php setlocale(LC_TIME, 'it_IT'); ?> How do I get a datetime in italian format? Thanks a lot I agree, you have to use the IntDateFormatter class, like @Jan Romero mentioned. Have a look at this thread, I made a few examples how to use the IntDateFormatter as one-liner, etc. 2
Jan Romero Posted January 9, 2023 Posted January 9, 2023 Nice. I’m all for one-liners (at least for something like formatting…), but I find the whole IntlDateFormatter business a bit verbose. I like to keep pre-configured instances of the things around, for example on $config, and just call something like $config->dateFormatter->format($page->cooldate) Not sure if $config is a good place for this, and I have no experience doing this on multilanguage sites, but I imagine you’d be able to prepare the instances according to the user’s language and never have to worry about it again. 2
Stefanowitsch Posted January 9, 2023 Posted January 9, 2023 15 minutes ago, Jan Romero said: Nice. I’m all for one-liners (at least for something like formatting…), but I find the whole IntlDateFormatter business a bit verbose. I like to keep pre-configured instances of the things around, for example on $config, and just call something like $config->dateFormatter->format($page->cooldate) Not sure if $config is a good place for this, and I have no experience doing this on multilanguage sites, but I imagine you’d be able to prepare the instances according to the user’s language and never have to worry about it again. Yes absolutely. I have this kind of monstrosities in my markup: <?= IntlDateFormatter::formatObject( new DateTime(date("d.m.Y", $event->date_event)), "EEEE, dd.MM.yy", 'de_DE' ); ?> Which works perfect but is a bit hard to read and to maintain (in fact I never want to touch this code again...). If you are working with a predefined date format all over your site it really makes sense to preconfig that. 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now