【发布时间】:2015-01-30 17:57:22
【问题描述】:
我有两个各自格式的日期(02-Dec-14 和 17-Dec-14),我想在 smarty 中比较这两个日期。
如何比较这两个日期?请帮忙。
提前致谢
【问题讨论】:
-
你自己尝试过什么吗??
我有两个各自格式的日期(02-Dec-14 和 17-Dec-14),我想在 smarty 中比较这两个日期。
如何比较这两个日期?请帮忙。
提前致谢
【问题讨论】:
这是我的 smarty 函数(添加到你的 smarty 插件目录):
/*
* Smarty plugin
* -------------------------------------------------------------
* Type: function
* Name: date_diff
* Author: Rafał Pawlukiewicz
* 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;
}
}
【讨论】:
我尝试了很多方法,无法得到解决方案。最后我尝试了这样的事情: 在 PHP 中,将 2 个类似这样的变量传递给 smarty:
$insuranceStartDate= date("jS F Y", strtotime($startdate));
$smarty->assign('insuranceStartDate' , $insuranceStartDate);
$insuranceStartDatePlain= date("Y/m/d", strtotime($startdate));
$smarty->assign('insuranceStartDatePlain' , $insuranceStartDatePlain);
智能比较如下:
{if $insuranceStartDatePlain|date_format:'%Y/%m/%d' < '2017/09/01'}
less than - 1<sup>st</sup> Septemeber 2017
{else}
greater - {$insuranceStartDate}
{/if}
注意:要比较日期,当它们是字符串时,在 smarty 中使用顺序 YYYY/MM/DD
希望它对将来的人有所帮助:)
【讨论】:
{if {$date1|date_format:"%y%m%d"} lt {$date2|date_format:"%y%m%d"}}
【讨论】:
例如,如果您将这些日期分配给 Smarty:
$smarty->assign('date2','02-Dec-14');
$smarty->assign('date1','17-Dec-14');
您可以直接在Smarty中使用strtotime函数,例如:
{if $date1|strtotime < $date2|strtotime}
{$date1} is earlier than {$date2}
{elseif $date1|strtotime == $date2|strtotime}
Both dates are the same
{else}
{$date2} is earlier than {$date1}
{/if}
【讨论】:
$php_modifiers 设置。
<?php
$d = '02-Dec-14';
$e = '17-Dec-14';
$t1 = strtotime($d);
$t2 = strtotime($e);
/*
// Test if it works.
if ($t2 > $t1) {
echo 'second is greater';
}
*/
然后您可以将其分配给 Smarty:
$this->assign('date_one', $t1);
$this->assign('date_two', $t2);
在 Smarty 中,您可以进行比较:
{if $date_one < $date_rwo}
...do something..
{/if}
【讨论】: