如何使用 Twig 格式化当前日期和时间?
How to format current date and hour with Twig?
使用 Twig 3 和 twig-intl :
dump(date('now'))
return 当前 DateTime 对象 : good ;
dump(date('now')|format_date('full'))
return 字符串日期 : good ;
dump(date('now')|format_date('full', 'none'))
return 空字符串 ""
;为什么?
dump(date('now')|format_date('full', 'short'))
return 奇怪的字符串 "118"
或 "348" or
"578"`, ... ;为什么?
我不明白为什么我不能显示当前日期和时间(完整、简短)。
我认为你混淆了 format_date() and format_datetime() 函数。
format_date() 文档说 "It behaves in the exact same way as the format_datetime filter, but without the time."
format_datetime() 文档说它允许第二个参数 none|short|medium|long|full 对于时间,对于 format_date函数。
dump(date('now')|format_date('full', 'none')) return empty string "" ;
why ?
=> 因为第二个参数不正确。可能的参数是 dateFormat、locale 和 pattern 类型。
dump(date('now')|format_date('full', 'short')) return strange string
"118" or "348" or"578"`, ... ; why ?
=> 同理。
I don't understand why I can't display the current date and time
(full, short).
=> 使用 format_datetime() 代替:
{{ date('now')|format_datetime('full', 'none') }}
{{ date('now')|format_datetime('full', 'short') }}
希望对您有所帮助。
使用 Twig 3 和 twig-intl :
dump(date('now'))
return 当前 DateTime 对象 : good ;dump(date('now')|format_date('full'))
return 字符串日期 : good ;dump(date('now')|format_date('full', 'none'))
return 空字符串""
;为什么?dump(date('now')|format_date('full', 'short'))
return 奇怪的字符串"118"
或"348" or
"578"`, ... ;为什么?
我不明白为什么我不能显示当前日期和时间(完整、简短)。
我认为你混淆了 format_date() and format_datetime() 函数。
format_date() 文档说 "It behaves in the exact same way as the format_datetime filter, but without the time."
format_datetime() 文档说它允许第二个参数 none|short|medium|long|full 对于时间,对于 format_date函数。
dump(date('now')|format_date('full', 'none')) return empty string "" ; why ?
=> 因为第二个参数不正确。可能的参数是 dateFormat、locale 和 pattern 类型。
dump(date('now')|format_date('full', 'short')) return strange string "118" or "348" or"578"`, ... ; why ?
=> 同理。
I don't understand why I can't display the current date and time (full, short).
=> 使用 format_datetime() 代替:
{{ date('now')|format_datetime('full', 'none') }}
{{ date('now')|format_datetime('full', 'short') }}
希望对您有所帮助。