【问题标题】:If statment with date create in smart php如果在智能 php 中创建带有日期的语句
【发布时间】:2020-10-08 13:18:54
【问题描述】:

如何在 smarty php 中创建验证,允许在指定日期后 30 天内显示该值?

在数组中:

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

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

看起来像这样:

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

【问题讨论】:

  • 您确定要在您的模板中执行此操作吗? stackoverflow.com/questions/27250242/… 对你有帮助吗?
  • 嗯.. 不,因为他有两个特定的日期,我需要从当前日期开始间隔 30 天。
  • 您可以将 DateTime 类与 diff() 方法一起使用 - if ((new DateTime)->diff(new DateTime($invoice['normalisedDateCreated ']))->days > 30)
  • @Qirel 不要使用 smarty php。
  • “不工作”是如此含糊,无法帮助解决问题。 确实 工作,它的原生 PHP。没有任何错误消息,真的不可能说出它为什么“不起作用”。也许应该是$invoice[0]['normalisedDateCreated'],但这是基本的 PHP,我希望您至少尝试找出什么不起作用。

标签: php laravel smarty smarty3


【解决方案1】:

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

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

但是如果可以从 php 传递另一个比当前日期早 30 天的变量,那将会简化。

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

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

【讨论】:

  • 他的回答以这种方式发挥了应有的作用:{if "$smarty.now -30 Days"|date_format:'%Y-%m-%d' &lt; $invoice.normalisedDateCreated}
  • 好朋友,再次帮助我.. 在您插入答案之前,我找到了另一个解决方案:{if strtotime($invoice.normalisedDateCreated) &gt; strtotime('-30 days')}
  • 很好,感谢您指出缺少if 关键字。也更新了比较运算符。
  • 比较符号也是反的,正确的应该是:&lt;。至少这种方式对我有用。
  • 兄弟,你不知道我在其他问题中所做的工作,如果你想看看最终结果如何,我在聊天中给你发了一个打印。哈哈再次感谢..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-30
  • 1970-01-01
  • 1970-01-01
  • 2021-11-30
相关资源
最近更新 更多