unistake Posted December 7, 2015 Share Posted December 7, 2015 (edited) Hi guys, I'm trying to calculate where a future date runs in a 15 day cycle pattern, and then if the day lands on day 13 or 15 of the cycle pattern I want to do something with the result. For example: $pattern = 15; $start_date = strtotime("2015-12-14"); // day 1 // Day 15 = 2015-12-29 $chosen_date = strtotime("2016-03-30"); $difference = ($chosen_date - $start_date) / 83400; // Find which Day the 'chosen_date' comes in the sequence and... If($chosen_date == 13 || $chosen_date == 15) { Echo 'possible outcome. Day $chosen_date."; } Else { Echo 'not possible, day $chosen_date'; } Hope it makes sense! Cheers Edited December 7, 2015 by unistake Quote Link to comment https://forums.phpfreaks.com/topic/299672-find-n-in-algorithm/ Share on other sites More sharing options...
requinix Posted December 8, 2015 Share Posted December 8, 2015 Okay, so I think I figured out what you want. $start_date is the first date. You may have picked it arbitrarily, I don't know. 14 days later is the end of that cycle (1+14=15), and 15 days later (2015-12-30) is actually day 1 of the next cycle. Now you have a $chosen_date. That's going to be 15 * k + d inclusive days between it and the starting date, where k is the number of complete cycles between them and d would be the day number. As an example, with 2015-12-31 it's 17 = 15 * (1) + (2), meaning 1 complete cycle and the date is day #2 in the cycle. If the chosen date is 2016-03-30, that's 108 days later or day #109 (1+108) of the cycle. 109 = 15 * (7) + 4 and so the date is day #4 of whatever cycle. Right? Quote Link to comment https://forums.phpfreaks.com/topic/299672-find-n-in-algorithm/#findComment-1527670 Share on other sites More sharing options...
unistake Posted December 8, 2015 Author Share Posted December 8, 2015 Hi Requinix, Thanks for your post. You are absolutely right in your logic and a much better explanation than mine! I'm just stuck putting this maths in to php! Quote Link to comment https://forums.phpfreaks.com/topic/299672-find-n-in-algorithm/#findComment-1527672 Share on other sites More sharing options...
Solution requinix Posted December 8, 2015 Solution Share Posted December 8, 2015 Alright. Use a DateTime for the chosen date and diff it to starting date. That will give you a DateInterval which can tell you how many days are between the two dates. Since you don't care about the number of cycles between the two dates (k from before) all you have to do is use modulus to get the day number (d). Which needs a bit more of an explanation. Say the diff is 108 days. The most obvious course of action is to +1 (because the starting date is day #1 and not day #0) and %15 to get 4. That's not quite right. Say the diff is 14 days. Smaller number. +1 is 15 and %15 is 0. That's not right: you need to get 15 and not 0. You could say "if result == 0 then result = 15" but there's an easier way: ($diff % 15) + 1What you're actually doing is taking the diff, +1 because of the starting date, then doing a trick where you -1 then %15 then +1. Watch: (14 % 15) + 1 = 15which is the result you wanted. And (108 % 15) + 1 = 4like before. $pattern = 15; $start_date = new DateTime("2015-12-14"); // day 1 // Day 15 = 2015-12-29 $chosen_date = new DateTime("2016-03-30"); $difference = $chosen_date->diff($start_date)->days; $day_number = ($difference % $pattern) + 1; If($day_number == 13 || $day_number == 15) { Echo "possible outcome. Day $day_number."; } Else { Echo "not possible, day $day_number"; } 1 Quote Link to comment https://forums.phpfreaks.com/topic/299672-find-n-in-algorithm/#findComment-1527673 Share on other sites More sharing options...
unistake Posted December 8, 2015 Author Share Posted December 8, 2015 Thanks Requinix, Makes great sense! I had a problem when it came to thinking about the number of cycles 'k' and didnt know the best way to do this, I was thinking making the difference of dates as a percentage and ignoring the number of cycles that would come before the decimal point. I'll post the script when I make it up later today Cheers Quote Link to comment https://forums.phpfreaks.com/topic/299672-find-n-in-algorithm/#findComment-1527675 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.