phplaravelsmartysmarty3

If statment with date create in smart php


How can I create a validation in smarty php, allowing the value to appear if it is within 30 days of the specified date?

In array:

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

I don't know what would be the ideal field for this, but we can use datecreated or normalisedDateCreated.

It would look something like this:

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

Solution

  • You can get current date using $smarty.now and then subtract days 30 from that.

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

    But it would be simplified if can just pass another variable from php which is 30 days prior to current date.

    Another solution proposed by @zecaluis

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