หาก statment พร้อมวันที่สร้างใน smart php

Oct 08 2020

ฉันจะสร้างการตรวจสอบความถูกต้องใน 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}

คำตอบ

1 DarkKnight Oct 08 2020 at 20:57

คุณสามารถรับวันที่ปัจจุบันโดยใช้$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')}