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.
Here is my code:
function returnReferrals($type){
global $pdo;
$stmt = $pdo->prepare("SELECT * FROM referrals
LEFT JOIN members ON members.mem_id = referrals.ref_referral
WHERE ref_type = :type AND ref_referrer = :mem");
$stmt-> bindValue(':type', $type);
$stmt-> bindValue(':mem', userId());
$stmt-> execute();
$res = array();
while($result = $stmt->fetch()){
$res[] = $result;
}
return $res;
}
$referrals = returnReferrals($type);
$smarty->assign('referrals', $referrals);
referrals.tpl
{foreach $referrals as $refs}
<!-- Here I need to calculate days difference and have logic like below -->
{if $days > 2}Inactive{else}Active{/if}
{/foreach}
Now to get the $days I need to calculate difference between two dates where one date comes from database datetime field $refs.ref_last_click 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?
Here is my smarty function (add to your smarty plugin directory):
then your code: