phpdatetimesmarty

Get from a smarty variable the day of week


I want add condition to a variable if the day of this condition is a Monday, Example:

{if $order.details.order_date == monday}
 {$order.details.order_date|replace:'/':'-'|cat:' +8 days'|date_format:'%A, %e de %B del %Y'}
{/if}

How can I read from a variable if the date is a monday?


Solution

  • {if $order.details.order_date|date_format:'l' == 'Monday'}
    

    Edit: more options.

    Depending on your version of Smarty you can also try:

    {if $order.details.order_date|date_format:'%u' == 1}
    

    Where the result of the date_format filter is an "ISO-8601 numeric representation of the day of the week, 1 (for Monday) through 7 (for Sunday)", see strftime() for more information

    Alternatively you can also try to assign the value to a variable before testing it:

    {$dayOfWeek = {$order.details.order_date|date_format:'%u'}}
    {if $dayOfWeek == 1}
    

    Further Edit

    You can try to convert the variable and then apply the filter:

    {$dateYMD = {{$order.details.order_date|substr:6:4}|cat:'-'|cat:{$order.details.order_date|substr:3:2}|cat:'-'|cat:{$order.details.order_date|substr:0:2}}}
    {if $dateYMD|date_format:'%u' == 1}
    

    but you could receive some 'deprecated' messages from PHP if they are not disabled, since substr will be removed in future smarty releases.

    You can also try replacing the slashes with dashes, it seems to work but I do not garantee for that:

    {if $order.details.order_date|replace:'/':'-'|date_format:'%u' == 1}