比较smarty中的两个日期

时间:2014-12-02 13:07:27

标签: php date smarty

我有各自格式的两个日期(02-Dec-14和17-Dec-14),我想在smarty中比较这两个日期。

我如何比较这两个日期?请帮忙。

提前致谢

5 个答案:

答案 0 :(得分:3)

如果您将这些日期分配给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}

答案 1 :(得分:2)

{if {$date1|date_format:"%y%m%d"} lt {$date2|date_format:"%y%m%d"}}

答案 2 :(得分:0)

<?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}

答案 3 :(得分:0)

我试过很多方法,无法得到解决方案。最后我尝试了这样的事情: 在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订单

希望将来帮助某人:)

答案 4 :(得分:0)

这是我的 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;
    }

}