This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

JeroenvanRensen
JeroenvanRensen

Hello everyone,

I'm using the Rainlab.Blog-plugin and I want to show a custom date format on my website. The standard format is {{ post.published_at|date('M d, Y') }}, and the output is Apr 28, 2020.

I want to have it in Dutch, like so: 28 april 2020, or 17 mei 2019, or anything like that. I tried to add a new function in the code tab:

{{ custom_date_format(post.published_at|date('M d, Y')) }}

function custom_date_format($date) {
    return '28 april 2020';
}

But unfortunately this didn't work.

Does anyone know another way of doing it?

Thank you!

mjauvin
mjauvin

You can use this in your Plugin's registration file:

public function registerMarkupTags()
{
    $twig = $this->app->make('twig.environment');
    $twigDate = "twig_date_format_filter";

    return [
        'filters' => [
            'ldate' => function($date, $format = null, $timezone = null ) use ($twig, $twigDate) {
                $date = Argon::parse($date);
                return $twigDate($twig, $date, $format, $timezone);
            },  
        ],  
    ];  
}

then use this in your twig markup:

{{ post.published_at | ldate('M d, Y') }}

Last updated

1-2 of 2

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.