【问题标题】:smarty and date聪明和约会
【发布时间】:2011-03-15 23:56:55
【问题描述】:

我的约会对象是:{$smarty.now|date_format:'%Y-%m-%d %H:%M:%S'}

但是如何在 20 天后获得?

如果现在:2010 05 05 12:12:12,我希望显示2010 25 05 12:12:12

【问题讨论】:

    标签: smarty


    【解决方案1】:

    使用strtotime() php 函数并将您的变量分配给smarty。像这样的:

    <?php
    $later = strtotime('+20 day');
    $smarty->assign('later', $later);
    ?>
    

    然后在模板中:

    { $later|date_format:'%Y-%m-%d %H:%M:%S'}
    

    【讨论】:

    • 谢谢,但我不能使用 PHP 代码。也许是只有 Smarty 的决定?
    • 现在还有其他完全依赖 smarty 且不需要 PHP 代码的答案
    【解决方案2】:

    {$smarty.now} 是一个简单的时间戳(自 1970 年以来的秒数)。因此,您可以根据需要添加任意多的秒数:

    {$smarty.now+20*24*60*60|date_format:'%Y-%m-%d %H:%M:%S'} //+20 days
    

    这在 Smarty3 中有效,如果不是在旧版本中,那么您可能需要使用 {assign} 和/或 {math} 指令进行数学运算。

    【讨论】:

      【解决方案3】:

      你可以直接使用 strtotime() 作为修饰符。

      {"+20 days"|strtotime|date_format:"Y/m/d"}
      

      【讨论】:

        【解决方案4】:

        在较新版本的 smarty 中,它将对您添加的任何字符串进行 strtotime

        即除了{$smarty.now|date_format:'%Y-%m-%d %H:%M:%S'} 你也可以{"now"|date_format:'%Y-%m-%d %H:%M:%S'}

        要获取从现在起 20 天后的日期,您可以:

        {"+20 days"|date_format:"%Y-%m-%d"}

        【讨论】:

        • 你拯救了我的一天)
        【解决方案5】:

        在 smarty 中测试:在动态日期中添加 1 天,2 天 ......365 天。

        $one= date("Y-m-d", strtotime(date("Y-m-d", strtotime('$add dynamic date variable')) . " + 1 day"));
          $this->smarty->assign('one',$one);
        
        $two= date("Y-m-d", strtotime(date("Y-m-d", strtotime('$add dynamic date variable')) . " + 2 day"));
          $this->smarty->assign('two',$two);
        ...
        ..
        
        $oneyear= date("Y-m-d", strtotime(date("Y-m-d", strtotime('$add dynamic date variable')) . " + 365 day"));
          $this->smarty->assign('oneyear',$oneyear);
        

        【讨论】:

          【解决方案6】:
          {assign var="iItemOne" value=$smarty.now}
          {assign var="iItemTwo" value=1296000} //60*60*24*15-> for 15 days
          {assign var="iSum" value=$iItemOne+$iItemTwo}
          
          {$iSum|date_format:'%Y-%m-%d %H:%M:%S'}
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-11-24
            • 2010-10-29
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多