M
M
Messi2016-11-01 10:41:42
Yii
Messi, 2016-11-01 10:41:42

Date format on Select in ActiveRecord?

In the model, I make a request to the database, I get all the data that I need and the date in the format 2016-10-02 09:45:25+03 .

$query = (new \yii\db\Query())->
                select('cd.date day,
                c.host_id, cd.customer_id')
            ->from('customer_download cd')
            ->leftJoin('customer c', '"c"."id" = "cd"."customer_id"')

But I need to bring this date to the form 2016-10-02 09:45:25 . In the query, I did it like this:
to_char(cd.date, 'YYYY-MM-dd HH24:MI') as day
But when I insert this code in the first query to_char(cd.date, 'YYYY-MM-dd HH24:MI') day, I get a syntax error at or near "AS"
How to do it correctly ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2016-11-01
@FitTech

$query = new \yii\db\Query();
$query ->select([
    'day' => "to_char(cd.date, 'YYYY-MM-dd HH24:MI')",
    'c.host_id', 
    'cd.customer_id'
])
->from('customer_download cd')
->leftJoin('customer c', '"c"."id" = "cd"."customer_id"')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question