B
B
bohdaniel2014-05-17 15:14:58
PHP
bohdaniel, 2014-05-17 15:14:58

PHP event calendar - fetch from database?

Hello.
You need to implement a calendar of events. The bottom line is to highlight the event with a link on a specific day of the month, if such a number lies in the range.
MYSQL
id start_date end_date
1 2014-05-06 2014-05-08
2 2014-02-06 2014-09-08
3 2014-04-12 2014-04-01
4 2014-05-01 2014-05-03
5 2014 -09-06 2014-09-11
6 2014-05-06 2014-05-25
.......
I found this solution: make a loop from 1 to 31 ($mydate), that is, go through all the days of the month and make something like this:
SELECT id FROM dates WHERE '$mydate' BETWEEN start_date AND end_date LIMIT 1;
It doesn't matter to me how many events are in this range, the main thing is to find out if there is any event for a certain number.
But it seems to me that it is inappropriate to loop through the loop and make 30 samples every time. In addition, the database can contain several thousand values. I suspect it will take a long time.
What is the easiest way to accomplish this task?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yago, 2014-05-17
@bohdaniel

Use timestamp in date fields
www.unixtimestamp.com/index.php Adding to
the point:
you are selecting all records in a range. When forming a calendar, you form days in a cycle. When forming days, check the presence of events for the current date in the records of the range. It is easier to convert all date values ​​to timestamp for convenient work with ranges.

N
Nazar Mokrinsky, 2014-05-17
@nazarpc

You can use database denormalization, add columns with day, month and filter by them if necessary. If these columns have an index, it should work quickly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question