Jump to content

Find n in algorithm


Go to solution Solved by requinix,

Recommended Posts

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 by unistake
Link to comment
https://forums.phpfreaks.com/topic/299672-find-n-in-algorithm/
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/299672-find-n-in-algorithm/#findComment-1527670
Share on other sites

  • Solution

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) + 1
What 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 = 15
which is the result you wanted. And

(108 % 15) + 1 = 4
like 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";
}
  • Like 1
Link to comment
https://forums.phpfreaks.com/topic/299672-find-n-in-algorithm/#findComment-1527673
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/299672-find-n-in-algorithm/#findComment-1527675
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.