Note that there are some explanatory texts on larger screens.

plurals
  1. PORepeating calendar events and some final maths
    primarykey
    data
    text
    <p>I am trying to have a go at the infamous repeating events on calendars using PHP/MySQL. I've finally found something that seems to work. I found my <a href="https://stackoverflow.com/a/5186095/158126">answer here</a> but I'm having a little difficulty finishing it off.</p> <p>My first table 'events'.</p> <pre><code>ID NAME 1 Sample Event 2 Another Event </code></pre> <p>My second table 'events_meta that stores the repeating data.</p> <pre><code>ID event_id meta_key meta_value 1 1 repeat_start 1336312800 /* May 7th 2012 */ 2 1 repeat_interval_1 432000 /* 5 days */ </code></pre> <p>With repeat_start being a date with no time as a unix timestamp, and repeat_interval an amount in seconds between intervals (432000 is 5 days). </p> <p>I then have the following MySQL which I modified slightly from the above link. The timestamp used below (1299132000 which is 12th May 2012) is the current day with no time.</p> <pre><code>SELECT EV.* FROM `events` EV RIGHT JOIN `events_meta` EM1 ON EM1.`event_id` = EV.`id` RIGHT JOIN `events_meta` EM2 ON EM2.`meta_key` = CONCAT( 'repeat_interval_', EM1.`id` ) WHERE EM1.meta_key = 'repeat_start' AND ( ( CASE ( 1336744800 - EM1.`meta_value` ) WHEN 0 THEN 1 ELSE ( 1336744800 - EM1.`meta_value` ) / EM2.`meta_value` END ) ) = 1 </code></pre> <p>In the above MySQL, the following code deducts the repeat_start field (<code>EM1.'meta_value'</code>) from the current date and then divides it by the repeat interval field (<code>EM2.'meta_value'</code>).</p> <pre><code>ELSE ( 1336744800 - EM1.`meta_value` ) / EM2.`meta_value` </code></pre> <p>OR</p> <pre><code>TODAYS DATE - START DATE / 5 DAYS </code></pre> <p>So here's the maths:</p> <pre><code>1336744800 - 1336312800 = 432000 432000 / 432000 = 1 </code></pre> <p>Now that works perfect. But if I change the current timestamp 5 days ahead to 1336312800 which is 17th Mat 2012, it looks a bit like this:</p> <pre><code>1336312800 - 1336312800 = 864000 86400 / 432000 = 2 </code></pre> <p>Which doesn't work because it equals 2 and in the MySQL it needs to equal 1. So I guess my question is, how do I get the MySQL to recognise a whole number rather than having to do this?</p> <pre><code>... WHERE EM1.meta_key = 'repeat_start' AND ( ( CASE ( 1336744800 - EM1.`meta_value` ) WHEN 0 THEN 1 ELSE ( 1336744800 - EM1.`meta_value` ) / EM2.`meta_value` END ) ) = IN (1,2,3,4,5,6,7,8,....) </code></pre> <p>Hope I'm making sense and I hope it's just a simple maths thing or a function that MySQL has that will help :) Thanks for your help!</p> <p><strong>EDIT: THE ANSWER</strong></p> <p>Thanks to @eggypal below, I found my answer and of course it was simple!</p> <pre><code>SELECT EV.* FROM elvanto_calendars_events AS EV RIGHT JOIN elvanto_calendars_events_meta AS EM1 ON EM1.`event_id` = EV.`id` RIGHT JOIN elvanto_calendars_events_meta AS EM2 ON EM2.`meta_key` = CONCAT( 'repeat_interval_', EM1.`id` ) WHERE EM1.meta_key = 'repeat_start' AND ( ( 1336744800 - EM1.`meta_value` ) % EM2.`meta_value`) = 0 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload