Blackjack
Blackjack

Reputation: 1065

Yii2 only show data that has attached time less than current time

How do I can select data to show based on comparation in yii2 Activerecord? My data record from db contain column depTime. So I want to show only data that has depTime less than current time. Here is my function in my controller.

public function actionSelectedTeam($id) {
    $searchModel = new TeamSearch();
    $dataProvider= $searchModel->search(Yii::$app->request->queryParams);
    $dataProvider->pagination = [
        'pageSize' => 5
    ];

    return $this->render('team-info', [
                'model' => $this->findModel($id),
                'dataProvider' => $dataProvider,
    ]);
}

Or there is another way to do that? I mean outside this method, maybe from the model.

Thankyou.

Upvotes: 0

Views: 242

Answers (1)

Muhammad Omer Aslam
Muhammad Omer Aslam

Reputation: 23740

You will always have the records displayed that are smaller than the current time, other than they are some kind of reservations and you are providing future date/time manually.

You haven't specified what is the type of the depTime column, I assume that you have a datetime column, you can use the time() and now() function to achieve this.

You can add the following line in your search model's search() method before you return the $dataProvider which is subtracting the depTime from the current time and if the result is positive it will include the record.

$query->andFilterWhere(['>', new Expression('time(now()) - time(depTime)'), 0]);

Upvotes: 1

Related Questions