Smarty template engine date output

Hello. There is a code that takes the date and time in unix format from the database and converts it to a normal date. Please tell me how to add the code if the date is today, it displays "today" if the date is yesterday, it displays "yesterday" and in other cases it just writes the date?

<div class="date_public">{%$message.date_add|date_format:"%e %B "|replace:'January':'января'|replace:'February':'февраля'|replace:'March':'марта'|replace:'April':'апреля'|replace:'May':'мая'|replace:'June':'июня'|replace:'July':'июля'|replace:'August':'августа'|replace:'September':'сентября'|replace:'October':'октября'|replace:'November':'ноября'|replace:'December':'декабря'%}</div>
Author: Дмитрий, 2018-04-15

1 answers

Your footcloth code is hardly convenient to work with. I'm not sure that the question is still relevant, but it's better to go about this way (the code is approximate):

{if $message.date_add|date_format == $smarty.now|date_format}
     {$txt = "сегодня"}
{elseif $message.date_add|date_format == strtotime('-1 day')|dateformat}
     {$txt = "вчера"}
{else}
     {$m = $message.date_add|date_format:"%m"}
     {$months = ["января", "февраля" .... ]}
     {$txt = ($message.date_format:"%e ".$monts[$m-1])}
{/if}

<div class="date_public">{$txt}</div>

You probably have an old smarty, so you should use {assign } instead of just assigning {$txt = ..}.

In general, with repeated use, such things, of course, should be taken out either in the plugin, or hidden in a nested template.

In the case of a plugin, you write this logic in a convenient way on the pcp, and then you will use as well as other modifiers, for example, {$message.date_add|pretty_date}

It will be something like this:

function smarty_modifier_pretty_date($d){
     if(date("Ymd", $d) == date("Ymd")) return "сегодня";
     ...
}
 1
Author: teran, 2018-05-03 08:49:29