1

On our store we have a blog where we show the "created_at" date of the entity. But we show the same post to 5 different languages. Of course the content is translated but we don't know how we should translate the date.

Currently we display it like this:

$date = date("d F, Y", strtotime($date)); // $date = 2019-04-16 08:45:25
echo "<span> " . $date . "</span>";

This outputs something like this:

enter image description here

Our main problem here is that the month is in english in all our languages. Is there a way to translate just the month?

PS: According to our PM just using the month number is not an option.

2 Answers 2

2

To format dates in other languages, you should use the setlocale() and strftime() functions.

<?php
setlocale(LC_TIME, "fr_FR"); // Here fr_FR is your translation locale
echo strftime("%d %B, %Y");
?>

Hope it finds helpful.

1
1

The method strftime() is deprecated as of PHP 8.1.0. Relying on this function is highly discouraged. As alternative, we can use the following code.

<?php

$formatter = new IntlDateFormatter('es_ES', IntlDateFormatter::LONG, IntlDateFormatter::NONE);
$dateTime = new DateTime('2022-01-30T15:09:09');
echo $formatter->format($dateTime);

//output: 30 de enero de 2022

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.