idontkno Posted February 14, 2010 Share Posted February 14, 2010 For example, you have February 1st as a day 0, 2nd as a day 1, 3rd as a day 0, 4th as a day 1, 5th as a day 0, weekends are skipped, 8th as a day 1, and so forth. However, there are occasional days where you skip them (and the day after that is the same as the one before it), e.g. every 5th Friday. How would I go about doing this? I originally used date() to determine if its a weekend or not and used a xor, but it doesn't work after you encounter a day where you skip it. Quote Link to comment https://forums.phpfreaks.com/topic/192002-determining-if-a-day-is-0-or-1/ Share on other sites More sharing options...
yozyk Posted February 14, 2010 Share Posted February 14, 2010 I am not sure that I understood you about «days where you skip them» for($i = 1, $st = 0; $i <= 28; $i++, $st = (int)!$st) { if(date('w', strtotime($i." February 2010")) > 5) $i += 2; $m[$i] = $st; } echo '<pre>',print_r($m, 1),'</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/192002-determining-if-a-day-is-0-or-1/#findComment-1012017 Share on other sites More sharing options...
idontkno Posted February 14, 2010 Author Share Posted February 14, 2010 I am not sure that I understood you about «days where you skip them» for($i = 1, $st = 0; $i <= 28; $i++, $st = (int)!$st) { if(date('w', strtotime($i." February 2010")) > 5) $i += 2; $m[$i] = $st; } echo '<pre>',print_r($m, 1),'</pre>'; Here, let me elaborate on that with another example: Feb 1 = 0 Feb 2 = 1 Feb 3 = 0 Feb 4 = 1 Feb 5 = skipped, but counts as 0 Feb 6 = weekend Feb 7 = weekend Feb 8 = 1 Feb 9 = 0 Feb 10 = 1 Feb 11 = skipped Feb 12 = 0 Feb 13 = weekend Feb 14 = weekend Feb 15 = 1 Feb 16 = 0 Feb 17 = 1 Quote Link to comment https://forums.phpfreaks.com/topic/192002-determining-if-a-day-is-0-or-1/#findComment-1012022 Share on other sites More sharing options...
salathe Posted February 14, 2010 Share Posted February 14, 2010 Why is Feb 11 skipped? Quote Link to comment https://forums.phpfreaks.com/topic/192002-determining-if-a-day-is-0-or-1/#findComment-1012038 Share on other sites More sharing options...
idontkno Posted February 14, 2010 Author Share Posted February 14, 2010 Why is Feb 11 skipped? Lets pretend its a global holiday like Christmas. Feb 5th is skipped because of maintenance on another server, but it still counts. Quote Link to comment https://forums.phpfreaks.com/topic/192002-determining-if-a-day-is-0-or-1/#findComment-1012241 Share on other sites More sharing options...
salathe Posted February 14, 2010 Share Posted February 14, 2010 Riiigghhhttt. So have you got a full definition of precisely which days behave normally (or, if more concise, which days to skip) as from the posts so far, none of us here have a firm starting point to help you from. Also, could I ask the whole purpose of this exercise? Quote Link to comment https://forums.phpfreaks.com/topic/192002-determining-if-a-day-is-0-or-1/#findComment-1012255 Share on other sites More sharing options...
idontkno Posted February 14, 2010 Author Share Posted February 14, 2010 Riiigghhhttt. So have you got a full definition of precisely which days behave normally (or, if more concise, which days to skip) as from the posts so far, none of us here have a firm starting point to help you from. Also, could I ask the whole purpose of this exercise? I have two schedules that run alternately every day on multiple servers. I figured the best way to do it was to define a day as a 0 or 1 and run the schedule respectively on those days. However, because there are occasional days where I need to take down a server for maintenance while the others are still running, I can't have one on day 0 and the rest on day 1. Of course, there are also days where everything gets shut down for an arbitrary reason and as a result it continues normally. Quote Link to comment https://forums.phpfreaks.com/topic/192002-determining-if-a-day-is-0-or-1/#findComment-1012260 Share on other sites More sharing options...
salathe Posted February 14, 2010 Share Posted February 14, 2010 Seems like an awfully complicated way to do something simple. Would the scheduler have enough information for it to determine whether the previous weekday was either 1 or 0 and whether it was skipped? Quote Link to comment https://forums.phpfreaks.com/topic/192002-determining-if-a-day-is-0-or-1/#findComment-1012268 Share on other sites More sharing options...
idontkno Posted February 14, 2010 Author Share Posted February 14, 2010 Seems like an awfully complicated way to do something simple. Would the scheduler have enough information for it to determine whether the previous weekday was either 1 or 0 and whether it was skipped? Yeah, I have the dates stored for when it should be skipped, but not for if it was 1 or 0. Actually, I originally thought of giving a day a 1 and then just xor the value every day. Quote Link to comment https://forums.phpfreaks.com/topic/192002-determining-if-a-day-is-0-or-1/#findComment-1012270 Share on other sites More sharing options...
Daniel0 Posted February 14, 2010 Share Posted February 14, 2010 Just flip it each day except weekends: <?php $month = 2; $year = 2010; $curDay = mktime(0,0,0,$month,1,$year); $numDays = (int) date('t', $curDay); $skip = array(5); $days = array(); for ($i = 1, $prev = 1; $i <= $numDays; ++$i, $curDay = strtotime('+1 day', $curDay)) { if (date('N', $curDay) >= 6) { $days[$i] = null; } else if (in_array($i, $skip)) { $prev = !$prev; $days[$i] = null; } else { $prev = $days[$i] = (int) !$prev; } } var_dump($days); Quote Link to comment https://forums.phpfreaks.com/topic/192002-determining-if-a-day-is-0-or-1/#findComment-1012320 Share on other sites More sharing options...
idontkno Posted February 14, 2010 Author Share Posted February 14, 2010 Just flip it each day except weekends: <?php $month = 2; $year = 2010; $curDay = mktime(0,0,0,$month,1,$year); $numDays = (int) date('t', $curDay); $skip = array(5); $jump = array(11); $days = array(); for ($i = 1, $prev = 1; $i <= $numDays; ++$i, $curDay = strtotime('+1 day', $curDay)) { if (date('N', $curDay) >= 6) { $days[$i] = null; } else if (in_array($i, $skip)) { $prev = !$prev; $days[$i] = null; } else { $prev = $days[$i] = (int) !$prev; } } var_dump($days); What about days where we skip it and not count it? Wait nevermind, I was stupid and didn't think. $month = 2; $year = 2010; $curDay = mktime(0,0,0,$month,1,$year); $numDays = (int) date('t', $curDay); $skip = array(5); $days = array(); for ($i = 1, $prev = 1; $i <= $numDays; ++$i, $curDay = strtotime('+1 day', $curDay)) { if (date('N', $curDay) >= 6) { $days[$i] = null; } else if (in_array($i, $skip)) { $prev = !$prev; $days[$i] = null; } else if (in_array($i, $pass)) { $days[$i] = null; } else { $prev = $days[$i] = (int) !$prev; } } var_dump($days); Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/192002-determining-if-a-day-is-0-or-1/#findComment-1012349 Share on other sites More sharing options...
Daniel0 Posted February 14, 2010 Share Posted February 14, 2010 So add an additional array for skip+no count and add an extra conditional in the if-chain in the loop. Quote Link to comment https://forums.phpfreaks.com/topic/192002-determining-if-a-day-is-0-or-1/#findComment-1012351 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.