title: Smarty how to calculate days difference between two dates
body: <p>How can I calculate difference between two dates and find the number of days in SMARTY template engine? I know the logic part should be done is PHP and assigned the value to SMARTY. But sometimes we land in a situation where we have to perform vice versa no matter what. I have to calculate this difference in smarty even though it defies the logic of using a template engine.</p>
  <p>Here is my code:</p>
  <pre><code>function returnReferrals($type){
    global $pdo;
  
    $stmt = $pdo-&gt;prepare(&quot;SELECT * FROM referrals
                           LEFT JOIN members ON members.mem_id = referrals.ref_referral
                           WHERE ref_type = :type AND ref_referrer = :mem&quot;);
    $stmt-&gt; bindValue(':type', $type);
    $stmt-&gt; bindValue(':mem', userId());
    $stmt-&gt; execute();
  
    $res = array();
    while($result = $stmt-&gt;fetch()){
      $res[] = $result;
    }
    return $res;
  }
  
  $referrals = returnReferrals($type);
  
  $smarty-&gt;assign('referrals', $referrals);
  </code></pre>
  <p>referrals.tpl</p>
  <pre><code>{foreach $referrals as $refs}
    
    &lt;!-- Here I need to calculate days difference and have logic like below --&gt;
  
    {if $days &gt; 2}Inactive{else}Active{/if}
  
  {/foreach}
  </code></pre>
  <p>Now to get the <code>$days</code> I need to calculate difference between two dates where one date comes from database <code>datetime</code> field <code>$refs.ref_last_click</code> and other is the current date. I could easily do this if I could in PHP but since I have assigned the looping variable to the template to be used in the foreach loop I have to now do it in SMARTY. How can I do this?</p>