Jump to content

[Solved] datetime and multi-language support


Laegnur
 Share

Recommended Posts

Hello

On my project, I'm using the multi-language module to translate the site to 3 languages. Now I want to format the posts datetime on a custom way, and I found this method using date() to output weekday name, month name, and day number. But the output names are on English.

What is the best way to set the locale for each language added in the multi-language module?

$data = strtotime($publicacion->data_publicacion);
$contido .= '<em>' . date('l', $data) . '</em>' . "\n";
$contido .= '<strong>' . date('F', $data) . '</strong>' . "\n";
$contido .= '<span>' . date('d', $data) . '</span>' . "\n";
Edited by Laegnur
Solved
Link to comment
Share on other sites

Hello

@bernhard thanks for your answer. Now I understand how to set the locale for each language in the module and was able to achieve what I wanted.

Finally my date code would look like this

$data = strtotime($publicacion->data_publicacion);

$dateFormatter = \IntlDateFormatter::create(
    $languages->getLocale(),
    \IntlDateFormatter::NONE,
    \IntlDateFormatter::NONE,
    \date_default_timezone_get(),
    \IntlDateFormatter::GREGORIAN
);

$contido .= '<time datetime="' . $publicacion->data_publicacion . '">' ."\n";
$dateFormatter->setPattern('EEEE');
$contido .= '<em>' . datefmt_format($dateFormatter, $data) . '</em>' . "\n";
$dateFormatter->setPattern('MMMM');
$contido .= '<strong>' . datefmt_format($dateFormatter, $data) . '</strong>' . "\n";
$dateFormatter->setPattern('dd');
$contido .= '<span>' . datefmt_format($dateFormatter, $data) . '</span>' . "\n";
$contido .= '</time>' . "\n";

And the final result:

2082721178_Capturadepantallaen2022-09-2623-52-39.png.6439ecbb42511a778ca64c21ec96a608.png381527223_Capturadepantallaen2022-09-2623-53-33.png.c4c2f95a221419f6a08967958ddbbe76.png206941884_Capturadepantallaen2022-09-2623-51-19.png.fd2b1e80e2e4a6655f436f5ccd705a0b.png

  • Like 2
Link to comment
Share on other sites

  • Laegnur changed the title to [Solved] datetime and multi-language support

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...