首页 > 解决方案 > 如果在智能 php 中创建带有日期的语句

问题描述

如何在 smarty php 中创建验证,如果该值在指定日期的 30 天内出现,则允许该值出现?

在数组中:

Array (4)
0 => Array (13)
  id => 6496
  invoicenum => 6496
  datecreated => "08/09/2020"
  normalisedDateCreated => "2020-09-08"

我不知道什么是理想的领域,但我们可以使用datecreatedor normalisedDateCreated

它看起来像这样:

{if $invoice.datecreated ... } if it is more than 30 days from the current date it should display NO
    YES
{else}
    NO
{/if}

标签: phplaravelsmartysmarty3

解决方案


您可以使用获取当前日期$smarty.now,然后从中减去 30 天。

{if "$smarty.now -30 Days"|date_format:'%Y-%m-%d' < $invoice.normalisedDateCreated}
    YES
{else}
    NO
{/if}

但是,如果可以只从当前日期前 30 天的 php 传递另一个变量,则会简化它。

@zecaluis 提出的另一个解决方案

{if strtotime($invoice.normalisedDateCreated) > strtotime('-30 days')}

推荐阅读