获取当月的最后一天

时间:2014-03-06 06:57:55

标签: date smarty

我可以使用cal_days_in_month(CAL_GREGORIAN, 03, 2014);函数在php中获取最后一天,然后将其分配给view。但我想知道是否有可能使用聪明的date_format函数获取当月的最后一天? 提前致谢

1 个答案:

答案 0 :(得分:0)

我不确定您是否只能使用date_format功能解决此问题。无论如何,我不会这样做,因为这会将太多的应用程序逻辑放入模板中。

但是......如果你想要这样做,只需添加一个template function即可得到你想要的东西。

PHP:

$tpl = new Smarty();
$tpl->registerPlugin("function","lastdayofmonth", "smarty_function_lastdayofmonth");
$tpl->display('your_template.tpl');

function smarty_function_lastdayofmonth($params, Smarty_Internal_Template $template) {
    return date("Y-m-t", strtotime($params['date']));
}

Smarty的:

{assign var='datevar' value='2014-03-06'}
{lastdayofmonth date=$datevar}

输出:

2014-03-31

请记住,这只是一个简单的例子。在Smarty中使用插件的方法不止一种。

相关问题