从一个smarty变量中获取星期几

huangapple go评论104阅读模式
英文:

Get from a smarty variable the day of week

问题

我想要为一个变量添加条件,如果这个条件所在的日期是星期一,
示例:

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

如何从一个变量中读取日期是否是星期一?

英文:

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?

答案1

得分: 2

  • 选项 1:
    同时从 PHP 传递计算出的星期几到 $order.details,与日期一起。

  • 选项 2:
    将筛选器应用于 $order.details.order_date:

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

编辑: 更多选项。

根据您的 Smarty 版本,您也可以尝试:

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

其中 date_format 过滤器的结果是 "星期的 ISO-8601 数字表示,1(代表星期一)到 7(代表星期日)",详见 strftime() 获取更多信息。

或者,您也可以尝试在测试之前将值分配给变量:

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

进一步编辑

您可以尝试转换变量,然后应用筛选器:

{$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}

但如果未禁用 substr,您可能会收到来自 PHP 的一些 "已弃用" 消息,因为 substr 将在未来的 Smarty 发布版本中移除。

您也可以尝试用破折号替换斜杠,似乎有效,但我不能保证其有效性:

{if $order.details.order_date|replace:'/':'-'|date_format:'%u' == 1}
英文:
  • Option 1:
    Also pass the calculated day of the week, along with the date, in $order.details, from PHP

  • Option 2:
    Apply a filter to $order.details.order_date:

{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}

huangapple
  • 本文由 发表于 2023年8月5日 05:31:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76839199.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定