Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

freeza's avatar

Date Format depends on language?

Hello Guys, i have a question to date formats in different languages.

In my Model i want to have a function like that:


public function created_at()
{
    return new DateHelper->format(new GermanFormat($this->created_at));
}

ok cool. But now i want to add "English" language to my app. So i created a new EnglishFormat class.

But now it sucks...

My Model function have to be like that:


public function created_at()
{
    // if DE THEN
    return new DateHelper->format(new GermanFormat($this->created_at));
    // if EN then
    //return new DateHelper->format(new EnglishFormat($this->created_at));
}

Do you have a better idea for me? I dont like the IF statements...

0 likes
4 replies
Rjs37's avatar
Rjs37
Best Answer
Level 2

In terms of localising the dates and any text within the date display, I make use of this package https://github.com/jenssegers/date which extends from Carbon.

For the format of the date itself, doing what Zach suggested could work very well. Just a single item called format, then just grab it in your model using the trans function.

ericlbarnes's avatar

I know this is late but another option is to use a JavaScript plugin like Moment.js. It can automatically detect the browsers timezone and convert it on the fly.

Here is a quick example:

<time datetime="{{ $model->created_at->toISO8601String() }}">

 {{ $model->created_at->toFormattedDateString() }}

</time>

Then with moment and jQuery:

$(function() {

    // Time conversion

    $("time").each(function(){

        var $el = $(this);

        var date = $el.attr("datetime");

        $el.text(moment(date).format("lll"));

    });

});

Please or to participate in this conversation.