Jump to content

Dates in Italian


Manaus
 Share

Recommended Posts

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

Link to comment
Share on other sites

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

  • Like 2
Link to comment
Share on other sites

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.

 

  • Like 2
Link to comment
Share on other sites

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.

  • Like 2
Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...