📜 ⬆️ ⬇️

How do you display the date depending on the location?

Good day to all. First of all, I would like to say thank you to everyone who took part in this survey. One way or another, it became clear that there is a sense in this kind of articles. So, below we will talk about the function that by chance I had to write, since there was no ready-made solution. Sobsvenno question itself - how do you display the date depending on the location? Interesting? I ask under the cat.

The function itself was written as a filter function to the twig template engine. Actually, the code itself

public function date2($date, $format = "EEEE d/MMMM/YYYY") { if(is_string($date)){ $date = new \DateTime($date); } $formatter = new \IntlDateFormatter(\Locale::getDefault(), \IntlDateFormatter::NONE, \IntlDateFormatter::NONE); $formatter->setPattern($format); return array('locale' => \Locale::getDefault(), 'intl' => $formatter->format($date)); } 


In principle, nothing difficult in it you will not find, but nevertheless, the code performs its role. At the input takes the date in the form of an object, but if a string comes in - then it turns into an object. At the exit - an array of keys. The first is the current location, the second date, in a format that you specify in the 2 passed argument of the function itself.
')
One nuance of this function is that it requires the intl library installed on the server.

In the next article, we will learn how to make your own filters for the twig template. Naturally with the direct participation of the Symfony2 framework. Good luck to everyone and pleasant coding. Love Symfony2 and may the force be with you.

PS There is still such a wonderful resource - userguide.icu-project.org/formatparse/datetime probably contains all possible date and time formats that can be found.

Source: https://habr.com/ru/post/205008/


All Articles