Jump to content

Get a list of todays events from a recurrence column


Go to solution Solved by gizmola,

Recommended Posts

Hi,

I haven't used PHP to create my own code for a few decades. Please go easy on me.

I am working with a MySQL database that runs a schedule. The schedule works from the recurrence column, with seven numbers (0 or 1). These numbers represent the days of the week (Monday to Sunday). 1s will trigger an event, 0s will not.

For example, if the recurrence column has 1000000, the task will only trigger on a Monday.

1100100 will trigger on Mondays, Tuesdays and Fridays

There are also fields for start date and end dates.

I would like to use PHP to extract the current days schedule. Can anyone provide advice on how this can achieved?

Example table

Recurrence     Startdate          Enddate              Descrption
1000000        20/02/2024      20/02/2030        Task1
1100100         28/02/2024      02/03/2024        Task 2

I'm confident that if someone can point me in a direction of "best approach", I can muddle my way through and learn along the way.

Thanks.

Edited by SamJ

Is there any chance whatsoever that you can change the nature of that column? As you've found out, storing the recurrence information with a number like that makes it very hard to work with, and there are far simpler and easier schemes you could make use of instead...

  • Solution

Without an actual spec for what the format of the output would be, here's a simple function that returns an array of the days indicated.

 

function toDayofWeekArray(string $schedule) {

    $days = [
      'Monday',
      'Tuesday',
      'Wednesday',
      'Thursday',
      'Friday',
      'Saturday',
      'Sunday'
    ];
    
    return array_combine($days, str_split($schedule));
}

 

It's important to note that this must be a string in the database, because, if you for example have this:   0000100 from the database, and PHP turns that into an integer, the function above won't work, because your leading zeros will be lost.  It must remain a string for this to work correctly.

 

Little test:

$i = "0001001";
var_dump(array_filter(toDayOfWeekArray($i)));

// Should return this
array(2) {
  ["Thursday"]=>
  string(1) "1"
  ["Sunday"]=>
  string(1) "1"
}

 

This is a simplified and combined version, that includes the filtration, and removes the left over array values:

 

function toDayofWeekArray(string $schedule) {

    $days = [
      'Monday',
      'Tuesday',
      'Wednesday',
      'Thursday',
      'Friday',
      'Saturday',
      'Sunday'
    ];
    
    return array_keys(array_filter(array_combine($days, str_split($schedule))));
}

 

Thank you @gizmola, that worked!

I've been reading information on the array functions you used; I feel like I am running before I have learned to walk!

I can print the days of the week each row will be trigged.

To extract the list of events the will be trigged today; my initial thought is to search the array against the current day. If today is in the array, then it will be trigged and should be displayed. The same logic can apply for the following days. 

I have successfully searched against an array without using a function with arraykeys.  But I am struggling to search the code you provided. Can you provide any advice how this can be achieved?

Thanks again. 

28 minutes ago, SamJ said:

Thank you @gizmola, that worked!

I've been reading information on the array functions you used; I feel like I am running before I have learned to walk!

I can print the days of the week each row will be trigged.

To extract the list of events the will be trigged today; my initial thought is to search the array against the current day. If today is in the array, then it will be trigged and should be displayed. The same logic can apply for the following days. 

I have successfully searched against an array without using a function with arraykeys.  But I am struggling to search the code you provided. Can you provide any advice how this can be achieved?

 

I would need some example data to understand what this list of events looks like.  There is no problem with you using a foreach loop or something like that, if it accomplishes your goal.  There are also functions like array_search that might be part of a solution.  

An easier approach is to search for those records with today's char in the recurrence column set to "1"

$sql = "SELECT id
             , recurrence
             , description
             , begindate
             , enddate
        FROM schedule
        WHERE ? BETWEEN begindate AND enddate
          AND SUBSTRING(recurrence, WEEKDAY(?)+1, 1)
          AND active
       ";
$res = $pdo->prepare($sql);

$searchDate = '2024-02-28';
$res->execute([$searchDate, $searchDate]);

Example outputs

searchdate = Feb 26 2024 (Monday)
+----+------------+-------------+------------+------------+
| id | recurrence | description | begindate  | enddate    |
+----+------------+-------------+------------+------------+
| 39 | 1010100    | Event1      | 2023-08-31 | 3000-01-01 |
+----+------------+-------------+------------+------------+


searchdate = Feb 28 2024 (Wednesday)
+----+------------+-------------+------------+------------+
| id | recurrence | description | begindate  | enddate    |
+----+------------+-------------+------------+------------+
| 39 | 1010100    | Event1      | 2023-08-31 | 3000-01-01 |
| 51 | 0010000    | Event2      | 2023-08-31 | 3000-01-01 |
+----+------------+-------------+------------+------------+

 

  • Great Answer 1

As usual Barand to the rescue with a really elegant solution.  Given what I understand about your needs, this is what I'd do.  I'd assume you need a job that takes an Event id and actually performs some action. 

  • Use Cron to run Barand's job 1x daily.
  • Loop through results and exec the event handler script(s) as required.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.