如何在fatfree模板中设置日期格式?
问题描述:
是否有可能以及如何使用它自己的函数在FatFree框架内格式化日期?如何在fatfree模板中设置日期格式?
<repeat group="{{ @rows }}" value="{{ @row }}">
<tr>
<td>{{ @row.idbox }}</td>
<td>{{ @row.code }}</td>
<td>{{ @row.createon }}</td>//date to format
<td>{{ @row.senton }}</td>
<td>{{ @row.price }}</td>
</tr>
</repeat>
答
该框架没有为日期格式提供专用过滤器。
格式筛选
可以使用format语法,但语法是有点特殊,因为它主要是为了本地化字符串:
本地化字符串:
index.php
$f3->PREFIX='dict.';
$f3->LOCALES('dict/');
$tpl=Template::instance();
echo $tpl->render('template.html');
dict/en.ini
order_date = Order date: {0, date}
template.html
<!-- with a UNIX timestamp -->
<td>{{ dict.order_date, @row.createon | format }}</td>
<!-- with a SQL date field -->
<td>{{ dict.order_date, strtotime(@row.createon) | format }}</td>
没有本地化字符串:
template.html
<!-- with a UNIX timestamp -->
<td>{{ '{0, date}', @row.createon | format }}</td>
<!-- with a SQL date field -->
<td>{{ '{0, date}', strtotime(@row.createon) | format }}</td>
自定义过滤器
幸运的是,框架给了我们创造0的可能性:
index.php
$tpl=Template::instance();
$tpl->filter('date','MyFilters::date');
echo $tpl->render('template.html');
myfilters.php
class MyFilters {
static function date($time,$format='Y-m-d') {
if (!is_numeric($time))
$time=strtotime($time);// convert string dates to unix timestamps
return date($format,$time);
}
}
template.html
<!-- default Y-m-d format -->
<td>{{ @row.createon | date }}</td>
<!-- custom format Y/m/d -->
<td>{{ @row.createon, 'Y/m/d' | date }}</td>
+0
很好的解释!谢谢 – andymo
答
使用标准的PHP date()函数。以前的答案是一个非常复杂的方式来获得相同的结果:
{{ date('d M Y',strtotime(@row.createon)) }}
你需要使用的strtotime是因为F3的::模板视图呈现变量,即使他们的时间戳/日期字符串中的原因数据库。
什么,如果有的话,你已经尝试过? – Adam
我看不懂.. – andymo
尝试'{{'{0,date}',@ row.createon |格式}}根据https://fatfreeframework.com/3.6/base#format – ikkez