【问题标题】:Compare two dates in smarty在 smarty 中比较两个日期
【发布时间】:2015-01-30 17:57:22
【问题描述】:

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

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

提前致谢

【问题讨论】:

  • 你自己尝试过什么吗??

标签: php date smarty


【解决方案1】:

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

}

【讨论】:

    【解决方案2】:

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

    希望它对将来的人有所帮助:)

    【讨论】:

      【解决方案3】:
      {if {$date1|date_format:"%y%m%d"} lt {$date2|date_format:"%y%m%d"}}
      

      【讨论】:

      • 它可以正常工作,除非您的第二次约会是在 100 年后,那么您最好使用 %Y%m%d 而不是 %y(2021 而不是 21)
      【解决方案4】:

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

      【讨论】:

      • 请注意,这可能需要您更改 Smarty 的 security settings,尤其是 $php_modifiers 设置。
      • @IMSoP True,但仅当您在 Smarty 中启用安全性时。默认情况下它未启用,因此您可能可以使用所有 PHP 函数/修饰符
      • 是的,只是认为如果有人尝试这样做但它不起作用,那么值得一提的是“陷阱”。
      • @IMSoP 是的,没错。我们不知道这里的 OP 代码是什么——可能是一些启用了安全性的 3rd 方代码
      • 谢谢大家,我在 lib 文件夹中制作了自定义插件,然后将它们调用到模板文件 {$smarty.now|strtotime|date_format:"%Y-%m-%d"}
      【解决方案5】:
      <?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}
      

      【讨论】:

        猜你喜欢
        • 2021-06-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-01
        • 2013-11-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多