D
D
Dmitry2017-02-02 00:54:41
MySQL
Dmitry, 2017-02-02 00:54:41

How to display date from timestamp in Russian?

Good evening.
There is a table that stores data about air travel and related tables that store data about countries and cities.
The task is the following.
The user enters a search query about flights in a certain period of time. Gets the result of a search, such as flights between February 2 and February 15 to Bangkok City. Under the search results, you need to display links that will display information about flights to this city in other months (existing).
Example:
"Bangkok in February", "Bangkok in March", "Bangkok in April" ....... "Bangkok in December"
Flight dates are stored in a timestamp. To pull the name of the month from the timestamp used 'FROM_UNIXTIME(flights.date_end, \'%M\'). This is to get unique titles, without repetition.
Here is the request

if(($model = self::find()->select(['FROM_UNIXTIME(flights.date_end, \'%M\') as month', 'flights.city_end_id', 'cities.city', 'cities.description'])
                                 ->distinct()
                                 ->leftJoin('cities', 'cities.id=flights.city_end_id')
                                 ->where(['city_end_id' => $id])
                                 ->andWhere(['>=', 'date_end', mktime(0,0,0,date('m'),date('d'),date('Y'))])
                                 ->asArray()
                                 ->all()) !== null){
            return $model;
        }
        else{
            return false;
        }

The request works, displays what is necessary. Here are just the names of the month in English.
How to translate it into Russian in this case.
PS Found a solution.
The problem was that I was getting the date wrong from the database. FROM_UNIXTIME should have been formatted as %Y-%m instead of %M .
Now dates are selected by months without repeating, but translating into Russian using Yii::$app->formatter is not difficult.
if(($model = self::find()->select(['FROM_UNIXTIME(flights.date_end, \'%Y-%m\') as month', 'flights.city_end_id', 'cities.city', 'cities.description'])
                                 ->distinct()
                                 ->leftJoin('cities', 'cities.id=flights.city_end_id')
                                 ->where(['city_end_id' => $id])
                                 ->andWhere(['>=', 'date_end', mktime(0,0,0,date('m'),date('d'),date('Y'))])
                                 ->asArray()
                                 ->all()) !== null){
            return $model;
        }
        else{
            return false;
        }

// И перевод на русский, при условии, что formatter правильно настроен
echo Yii::$app->formatter->format($model->month, ['date', 'MMMM']);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Eremin, 2017-02-09
@Sergei_Erjemin

I don't know how much advice will help (I don't practice PHP myself), but it seems to me that it's better to use date generation on the client using JS ... momentjs.com/docs It's easier to do cross-language output.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question