Answer the question
In order to leave comments, you need to log in
Yii Sampling from base on dates. What's wrong?
Hello!
There are
three
tables: appointment , profile , event ( appointments , profile, events) 2018-11-25 (duration 1 day) Format:
Dates in the database are stored in Ymd format I need to check for free user dates. The free date is determined by the event (table event). When assigning a user (appointment table) to an event, we select all appointments of the current user, except for the current event, and perform a search
for free dates according to the table with events (table event). If we find an appointment, event date range, or event date that matches/is included in the current event dates, we throw an error that the user is already busy on those dates.
I do it with this query:
$appointmentsInDates = Appointment::find()
->joinWith(['event', 'profile'])
->andWhere([Profile::tableName() . '.user_id' => $model->certification->user_id])
->andWhere(['!=', Event::tableName() . '.id', $model->event_id])
->andWhere(['>=', Event::tableName() . '.date_from', '2018-11-25'])
->andWhere(['<=', Event::tableName() . '.date_to', '2018-11-25'])
->groupBy(Event::tableName() . '.id')
->exists();
SELECT `appointment`.* FROM `appointment`
LEFT JOIN `event` ON `appointment`.`event_id` = `event`.`id`
LEFT JOIN `certification` ON `appointment`.`judge_comitet_id` = `certification`.`id`
LEFT JOIN `profile` ON `certification`.`user_id` = `profile`.`user_id`
WHERE (`profile`.`user_id`=139)
AND (`event`.`id` != 55)
AND (`event`.`date_from` >= 2018-11-25)
AND (`event`.`date_to` <= 2018-11-25)
GROUP BY `event`.`id`
ORDER BY `event`.`date_to`
Answer the question
In order to leave comments, you need to log in
If your date is stored as 2018-11-24 , then how can you compare them?
It turns out something like this
As far as I understand in any way.
I would add time to the comparison
But this may not work either, since the date in the database has no time.
Add time, even preferably in timestamp
Then you can compare like this:
Here is Event::tableName() . '.date_from', timestamp, this also produces timestamp strtotime('2018-11-24' . '00:00:00')
And the second date can be compared like this:
Something like this, if I'm not mistaken of course.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question