SamJ Posted February 25 Share Posted February 25 (edited) 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 February 25 by SamJ Quote Link to comment https://forums.phpfreaks.com/topic/318483-get-a-list-of-todays-events-from-a-recurrence-column/ Share on other sites More sharing options...
requinix Posted February 25 Share Posted February 25 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... Quote Link to comment https://forums.phpfreaks.com/topic/318483-get-a-list-of-todays-events-from-a-recurrence-column/#findComment-1616525 Share on other sites More sharing options...
SamJ Posted February 25 Author Share Posted February 25 Unfortunately, this is not possible. This is software created by a company, who's core software is based on the data. Quote Link to comment https://forums.phpfreaks.com/topic/318483-get-a-list-of-todays-events-from-a-recurrence-column/#findComment-1616528 Share on other sites More sharing options...
Solution gizmola Posted February 25 Solution Share Posted February 25 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)))); } Quote Link to comment https://forums.phpfreaks.com/topic/318483-get-a-list-of-todays-events-from-a-recurrence-column/#findComment-1616529 Share on other sites More sharing options...
SamJ Posted February 25 Author Share Posted February 25 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. Quote Link to comment https://forums.phpfreaks.com/topic/318483-get-a-list-of-todays-events-from-a-recurrence-column/#findComment-1616537 Share on other sites More sharing options...
gizmola Posted February 25 Share Posted February 25 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. Quote Link to comment https://forums.phpfreaks.com/topic/318483-get-a-list-of-todays-events-from-a-recurrence-column/#findComment-1616538 Share on other sites More sharing options...
SamJ Posted February 25 Author Share Posted February 25 Thank you for the pointer. I will keep chipping away. I have uploaded the SQL export of the table. If you have the time and energy any further help would be appreciated. Cheers sql.txt Quote Link to comment https://forums.phpfreaks.com/topic/318483-get-a-list-of-todays-events-from-a-recurrence-column/#findComment-1616539 Share on other sites More sharing options...
Barand Posted February 25 Share Posted February 25 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 | +----+------------+-------------+------------+------------+ 1 Quote Link to comment https://forums.phpfreaks.com/topic/318483-get-a-list-of-todays-events-from-a-recurrence-column/#findComment-1616549 Share on other sites More sharing options...
gizmola Posted February 27 Share Posted February 27 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. Quote Link to comment https://forums.phpfreaks.com/topic/318483-get-a-list-of-todays-events-from-a-recurrence-column/#findComment-1616652 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.