Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5966733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:50:31+00:00 2026-05-22T19:50:31+00:00

I am using an awful propriety CMS system. Its events module can store events

  • 0

I am using an awful propriety CMS system.

Its events module can store events by an explicit date/time, e.g. 2011-08-04 13:30:00 or by a weekly recurring event, of which the recurring day is stored as a 0 based integer and the time is added to the date/time field (where the data is 0), e.g. 0000-00-00 13:30:00.

I need to get all events upcoming in the next 2 weeks. This means I need all weekly recurring events and the explicit dates which fall in the next 2 weeks.

I’m not much of an SQL expert. I wrote this query…

SELECT `id`,
       `name`,
       `info`,
       `date_time`,
       `weekly_day`
FROM   `events` AS a
WHERE  `active` = true
       AND `id` IN (SELECT `id`
                    FROM   `events`
                    WHERE  WEEK(`date_time`) BETWEEN WEEK(NOW()) AND WEEK(
                                                     DATE_ADD(NOW(), INTERVAL
                                                     1 WEEK)))
        OR DATE(`date_time`) = 0
ORDER  BY Time_to_sec(IF(TIME(`date_time`), TIME(`date_time`),
                                            Concat(EXTRACT(HOUR FROM `date_time`
                                                   ), ":",
                                            EXTRACT(MINUTE FROM `date_time`)))),
          IF(DATE(`date_time`), Dayofweek(`date_time`), `weekly_day` + 1) ASC 

(Note that the NOW() above are replaced with some PHP with echoes the current date in the MySQL format. The MySQL server’s timezone is different to the timezone I want.)

This successfully gets all records I want, but it doesn’t order them correctly. For example, events on the 9th June are appearing before events on the 2nd June.

How can I fix my query?

Cheers.

Update

I’ve came up with this but I am still stuck… it won’t display the weekly events despite the dates appearing to be correct.

SELECT `id`,
       `name`,
       `info`,
       `date_time`,
       `weekly_day`,
       ( DATE(`date_time`) = 0 ) AS `weekly`
FROM   `events`
WHERE  `active` = true
       AND IF (DATE(`date_time`), WEEK(`date_time`),
           DATE_ADD(
               Concat(DATE(NOW()), " ", Concat(EXTRACT(HOUR FROM `date_time`),
                                        ":",
                                        EXTRACT
                                        (
                                        MINUTE FROM `date_time`))), INTERVAL
           `weekly_day`
                                                                    DAY))
           BETWEEN WEEK(
           NOW()) AND WEEK(DATE_ADD(NOW(), INTERVAL 2 WEEK))
ORDER  BY `date_time` ASC 

I couldn’t use TIME() to extract the time portion because it kept returning NULL.

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:50:32+00:00Added an answer on May 22, 2026 at 7:50 pm

    I would do it in two steps:

    1. Convert the recurring entries into the two DATETIME values for the next two weeks. This would be one sub-query.
    2. Collect the non-recurring entries from the same period. This would be a second sub-query.

    The UNION of the two sub-queries gives you all the events with their actual date and time of occurrence; it is trivial to order that correctly.

    If you need to record whether or not the information is from a recurring meeting, keep something (probably a simple ‘R’ or ‘N’) in the select-list of the two sub-queries so you can tell whether the original entry was recurring or non-recurring.


    I’m not an expert in MySQL DATETIME manipulation – but I do know my away around that area of another DBMS, namely Informix. For my purposes in this discussion, there are only two interesting columns in the table: date_time and weekly_day. I assume that you are using the MySQL convention that 1 = Sunday, 7 = Saturday. Since we need 2 weeks’ worth of information for recurring events, it is probably easiest to generate 3 weeks and to filter out the irrelevant ones.

    Given a reference date and an integer day of the week, we can get three values from it using:

     refdate - DAYOFWEEK(refdate) + day_of_week
     refdate - DAYOFWEEK(refdate) + day_of_week + INTERVAL  7 DAYS
     refdate - DAYOFWEEK(refdate) + day_of_week + INTERVAL 14 DAYS
    

    These yield three dates; the first might be in the past or the third might be too far in the future (and hence need discarding). This is the arithmetic when dates are a count of days since a reference date (as in Informix). To translate the third line into MySQL, assuming ‘refdate’ is NOW(), we seem to have to write (extraordinarily verbosely – I thought Informix was badly verbose for DATETIME manipulation):

    DATE_ADD(DATE_ADD(DATE_SUB(NOW(), DAYOFWEEK(NOW())), INTERVAL weekly_day DAY),
        INTERVAL 14 DAY)
    

    Here, I’m assuming that I can cast the integer in weekly_day to a number of days; adjust if necessary. These formulae give three DATE values for each of the recurring entries, of which two are relevant. To add the time, I think we need:

    DATE_ADD(DATE_ADD(DATE_ADD(DATE_SUB(NOW(), DAYOFWEEK(NOW())), INTERVAL weekly_day DAY),
        INTERVAL 14 DAY), INTERVAL TIME(date_time) HOUR_SECOND)
    

    So, the first sub-query mentioned in my initial answer itself needs to be a 3-way UNION.

    SELECT 'R' AS info_mode,
           DATE_ADD(DATE_ADD(DATE_ADD(DATE_SUB(NOW(), DAYOFWEEK(NOW())),
                                      INTERVAL weekly_day DAY),
                             INTERVAL 14 DAY),
                    INTERVAL TIME(date_time) HOUR_SECOND) AS event_time
      FROM events
    UNION
    SELECT 'R' AS info_mode,
           DATE_ADD(DATE_ADD(DATE_ADD(DATE_SUB(NOW(), DAYOFWEEK(NOW())),
                                      INTERVAL weekly_day DAY),
                             INTERVAL 7 DAY),
                    INTERVAL TIME(date_time) HOUR_SECOND) AS event_time
      FROM events
    UNION
    SELECT 'R' AS info_mode,
           DATE_ADD(DATE_ADD(DATE_SUB(NOW(), DAYOFWEEK(NOW())),
                                      INTERVAL weekly_day DAY),
                    INTERVAL TIME(date_time) HOUR_SECOND) AS event_time
      FROM events;
    

    You now need to filter that for ‘the next two weeks’. Which date/time range is that, exactly? From the reference moment – designated NOW()? Or from the beginning of the reference day? Or from the beginning of the day after the reference day? All could be plausible; since the code appears to use the reference mode (and it is simplest), we’ll go with that:

    SELECT *
      FROM (SELECT 'R' AS info_mode,
                   DATE_ADD(DATE_ADD(DATE_ADD(DATE_SUB(NOW(), DAYOFWEEK(NOW())),
                                              INTERVAL weekly_day DAY),
                                     INTERVAL 14 DAY),
                            INTERVAL TIME(date_time) HOUR_SECOND) AS event_time
              FROM events
            UNION
            SELECT 'R' AS info_mode,
                   DATE_ADD(DATE_ADD(DATE_ADD(DATE_SUB(NOW(), DAYOFWEEK(NOW())),
                                              INTERVAL weekly_day DAY),
                                     INTERVAL 7 DAY),
                            INTERVAL TIME(date_time) HOUR_SECOND) AS event_time
              FROM events
            UNION
            SELECT 'R' AS info_mode,
                   DATE_ADD(DATE_ADD(DATE_SUB(NOW(), DAYOFWEEK(NOW())),
                                              INTERVAL weekly_day DAY),
                            INTERVAL TIME(date_time) HOUR_SECOND) AS event_time
              FROM events) AS r
      WHERE r.event_time BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 14 DAY);
    

    This gives the recurring events. The filter condition on r.event_time could be pushed down (replicated) in each branch of the UNION, but I’ll leave it where it is, pro tempore.

    The non-recurring events are found using:

    SELECT 'N' AS info_mode,
           date_time AS event_time
      FROM events AS e
     WHERE e.date_time BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 14 DAY)
       AND DATE(e.date_time) != DATE '0000-00-00';
    

    Hence, we can create the union of those two queries. It is possible to drag the condition on event_time out of the two sub-queries (and we can hope that the optimizer then ‘puts it back’).
    And the non-recurring events sub-query simply becomes the fourth part of a UNION, leading to:

    SELECT *
      FROM (SELECT 'R' AS info_mode,
                   DATE_ADD(DATE_ADD(DATE_ADD(DATE_SUB(NOW(), DAYOFWEEK(NOW())),
                                              INTERVAL weekly_day DAY),
                                     INTERVAL 14 DAY),
                            INTERVAL TIME(date_time) HOUR_SECOND) AS event_time
              FROM events
            UNION
            SELECT 'R' AS info_mode,
                   DATE_ADD(DATE_ADD(DATE_ADD(DATE_SUB(NOW(), DAYOFWEEK(NOW())),
                                              INTERVAL weekly_day DAY),
                                     INTERVAL 7 DAY),
                            INTERVAL TIME(date_time) HOUR_SECOND) AS event_time
              FROM events
            UNION
            SELECT 'R' AS info_mode,
                   DATE_ADD(DATE_ADD(DATE_SUB(NOW(), DAYOFWEEK(NOW())),
                                              INTERVAL weekly_day DAY),
                            INTERVAL TIME(date_time) HOUR_SECOND) AS event_time
              FROM events
            UNION
            SELECT 'N' AS info_mode,
                   date_time AS event_time
              FROM events
             WHERE DATE(e.date_time) != DATE '0000-00-00'
           ) AS e
     WHERE e.event_time BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 14 DAY);
    

    So, that should give you the list of event types (recurring, non-recurring) and the ones that occur in the next 14 days. You just need to add back the id, name and info columns (plus, for debugging, the raw date_time and weekly_day columns) to each of the 4 queries in the UNION, and an ORDER BY clause on e.event_time (and any tie-breaking columns you want to add).

    Please be kind about any syntax or DBMS-specific semantic errors; I hope the concept is clear (and correct), even if there are details that are not correct because of unfamiliarity with the details of MySQL.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using online interfaces to a version control system is a nice way to have
Using PyObjC , you can use Python to write Cocoa applications for OS X.
Using C# and System.Data.SqlClient, is there a way to retrieve a list of parameters
Using VS2008, C#, .Net 2 and Winforms how can I make a regular Button
I am having awful trouble getting functional tests to work when using devise and
I've tried using SNNS but only on a Linux machine, and it's fairly awful.
Using active record, how can I return the results for a model if they
Using Lift's json parser, how can I output parsed json objects into a template?
I've recently started using lambdas an awful lot within threads, and want to make
Having an awful time trying to get the accurate time for a set of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.