Twig视图中的随机日期 [英] Random date in a view with Twig
本文介绍了Twig视图中的随机日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
出于好奇,我该如何使用 Twig
生成一个随机日期?
(用于改善测试页,新闻,博客等)
Out of curiosity, How can I generate a random date with Twig
?
(Its for improve the realisme of a test page, news,blog etc)
我知道如何显示实际日期,但我认为随机选择一个日期会更好
I know how to render the actual date,but I think it can be better with a random one
推荐答案
您可以扩展 twig
为您生成它:
You can extend twig
to generate it for you :
$function = new Twig_SimpleFunction('random_date', function() {
return mt_rand(time(), time() + 31556926);
});
$twig = new Twig_Environment($loader);
$twig->addFunction($function);
然后您可以在嫩枝
中使用它像这样:
And then you can use it in twig
like this :
{{ random_date()|date('d-m-Y') }}
这篇关于Twig视图中的随机日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文