該框架沒有爲日期格式提供專用過濾器。
格式篩選
可以使用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>
什麼,如果有的話,你已經嘗試過? – Adam
我看不懂.. – andymo
嘗試'{{'{0,date}',@ row.createon |格式}}根據https://fatfreeframework.com/3.6/base#format – ikkez