dongzi0602 2013-09-25 14:24
浏览 39
已采纳

在Smarty中创建指定月份的时间戳

I am populating a simple HTML select list in a Smarty template with month names. A timestamp object is passed to the template. I would like the passed in timestamp month to be the default value of the select. The passed value could be passed in as an int (1,2,3,etc...) or a string (jan,feb,mar,etc...).

To set a default, selected value I need to compare the passed in timestamp month to a timestamp I created in the template. For example, to compare to just the current month I would do something like this:

<option {if $smarty.now|date_format:"%b" = $passedInTimestamp['month']}selected{/if}>
    text
</option>

$smarty.now will only work for the current month obviously. My <option> tag is inside a for loop, looping from 1 to 12. How can I create a timestamp with a specific month to compare to the passed in month?

  • 写回答

1条回答 默认 最新

  • dongli8862 2013-09-26 04:07
    关注

    If I understood well, this is what you need :

    <select>
    {for $i=1 to 12}
        <option {if '1-'|@cat:$i:'-':'Y'|date|@strtotime|date_format:"%b" == $passedInTimestamp['month']}selected{/if}>
            {$i}
        </option>
    {/for}
    </select>
    

    First, I created a date string for months using cat modifier :

    {'1-'|@cat:$i:'-':'Y'|date}
    // will output 
    // 1-1-2013   1-2-2013   1-3-2013 ....
    

    Then I used strtotime modifier to convert it to timstamp

    {'1-9-2013'|@strtotime}
    // will output 1377986400
    

    Finally I used date_format:"%b" as you did above to compare it to your passed in timestamp month.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分