[英]Smarty how to calculate days difference between two dates
How can I calculate difference between two dates and find the number of days in SMARTY template engine?如何计算两个日期之间的差异并在 SMARTY 模板引擎中找到天数? I know the logic part should be done is PHP and assigned the value to SMARTY.我知道应该完成的逻辑部分是 PHP 并将值分配给 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.即使它违反了使用模板引擎的逻辑,我也必须在 smarty 中计算这种差异。
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推荐人.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.现在要获得$days
,我需要计算两个日期之间的差异,其中一个日期来自数据库datetime
时间字段$refs.ref_last_click
,另一个是当前日期。 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.如果我可以在 PHP 中做到这一点,我可以很容易地做到这一点,但是因为我已经将循环变量分配给要在 foreach 循环中使用的模板,所以我现在必须在 SMARTY 中做到这一点。 How can I do this?我怎样才能做到这一点?
Here is my smarty function (add to your smarty plugin directory):这是我的 smarty function(添加到你的 smarty 插件目录):
/*
* Smarty plugin
* -------------------------------------------------------------
* Type: function
* Name: date_diff
* Purpose: factor difference between two dates in days, weeks, or years
* Input: d1 = "mm/dd/yyyy" or "yyyy/mm/dd" or "yyyy-mm-dd"
* d2 = "mm/dd/yyyy" or "yyyy/mm/dd" or "yyyy-mm-dd" or $smarty.now
* assign = name of variable to assign difference to
* interval = "days" (default), "weeks", "years"
* Examples: {date_diff d1="2020-01-20"}
* Examples: {date_diff d1="2020-01-20" d2=2020-02-10 interval="weeks"}
* Examples: {date_diff d1="2020-01-20" d2=2020-02-10 assign="variable_diff"} result: {$variable_diff}
* -------------------------------------------------------------
*/
function smarty_function_date_diff($params, &$smarty)
{
$d1 = isset($params['d1']) ? $params['d1'] : date('Y-m-d');
$d2 = isset($params['d2']) ? $params['d2'] : date('Y-m-d');
$assign_name = isset($params['assign']) ? $params['assign'] : '';
$date1 = strtotime($d1);
$date2 = strtotime($d2);
// use current for empty string
if (! $date1) {
$date1 = date('Y-m-d');
}
if (! $date2) {
$date2 = date('Y-m-d');
}
$interval = isset($params['interval']) ? $params['interval'] : 'days';
// diff in days
$diff = ($date2 - $date1) / 60 / 60 / 24;
if ($interval === "weeks") {
$diff /= 7;
}
elseif ($interval === "years") {
$diff /= 365.25;
}
$diff = floor($diff);
if ($assign_name) {
$smarty->assign($assign_name, $diff);
}
else {
return $diff;
}
}
then your code:然后你的代码:
{foreach $referrals as $refs}
{date_diff d1=$refs.ref_last_click assign='days'}
{* 'd2' will appear as default current time *}
{if $days > 2}Inactive{else}Active{/if}
{/foreach}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.