2

I am working on PHP and MySQL based application. You can create events in it, make reservations and so on. Let's assume I have MySql table:

--table 'events'
+------+---------------------+---------------------+
| name |      startdate      |       enddate       |
+------+---------------------+---------------------+
| aaa  | 2014-10-28 00:00:00 | 2014-10-28 23:59:59 |
| bbb  | 2015-01-01 10:00:00 | 2015-01-01 16:00:00 |
| ccc  | 2014-12-24 17:00:00 | 2014-12-28 08:30:30 |
+------+---------------------+---------------------+

What I need is to get all dates, where the events take place. So, for events which start and end on the same day, I would like to get the single date, and for multiple day events, I would like to get the start date and then dates through the end date. So, the expected result would look like this:

+------------+
|   dates    |
+------------+
| 2014-10-28 |
| 2015-01-01 |
| 2014-12-24 |
| 2014-12-25 |
| 2014-12-26 |
| 2014-12-27 |
| 2014-12-28 |
+------------+

Of course, I have tried to search here and know, that there are similar questions answered, but they are just showing dates betwen given date, and do not take in mind 'one day' events.

Thanks in advance for any help.

EDIT: The reason to get this is to show on which days have any events in calendar view.

Salman A
  • 262,204
  • 82
  • 430
  • 521
Richard Šimek
  • 77
  • 2
  • 10

2 Answers2

1
$getEvents = mysqli_query("SELECT ...... You query");
$row_getEvents = mysqli_fetch_assoc($getEvents);

do {
    $date = date("Y-m-d", strtotime($row_getEvents['startdate']));
    $end_date = date("Y-m-d", strtotime($row_getEvents['enddate']));

    while (strtotime($date) <= strtotime($end_date)) {
        echo "$date\n";
        $date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
    }
} while ($row_getEvents = mysqli_fetch_assoc($getEvents));
Ghassan Elias
  • 2,213
  • 1
  • 14
  • 17
-1
SELECT name 
FROM events 
WHERE start BETWEEN ' 2014-10-28' AND '2014-10-28'

try this

Ferrakkem Bhuiyan
  • 2,741
  • 2
  • 22
  • 38