简体   繁体   中英

MySQL query the events for a calendar

I'm building a SaaS calendar with MySQL & PHP.

The front-end javascript calendar lib is fullcalendar(monthly view) http://fullcalendar.io/

I have a mysql data table named events like this:

  • ID
  • ID_USER
  • BODY_NAME
  • BODY_START
  • BODY_END
  • IS_DONE
  • TIME_CREATED
  • TIME_UPDATED
  • TIME_DELETED

The BODY_START and BODY_END is using unix_timestamp.

Then in my PHP code, I built a query helper that generate a SQL like:

"SELECT * FROM `" . $this->_tableName . "` WHERE `ID_ACCOUNT` = '$_id_account' AND (( `BODY_START` >= 1424649600 AND `BODY_START` <= 1428278400 ) OR (`BODY_END` >= 1424649600 AND `BODY_END` <= 1428278400))";

The start and end timestamp params provided by the fullcalendar.

There is a question:

if the event is crossing the current month, it will not be shown.

example:

  • ID f8a58d0c8280db2340863a1ac7aa60b0
  • ID_USER e576c22ce89f38ee422ec818807b99b8
  • BODY_NAME test_event
  • BODY_START 1422720000
  • BODY_END 1428422400
  • IS_DONE 0
  • TIME_CREATED 1422845255
  • TIME_UPDATED 0
  • TIME_DELETED 0

If you do the query, this event will not show.

how can i fix this? Thanks.

If I understand you correctly, simplify your query to be (in pseudo code):

  BODY_START <= month_end and BODY_END >= month_start

Your original query query above requires that events must either start or end during the month, but that's not actually what you want - you want any event that exists for any part of the month.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM