Jump to content

Determining if a day is 0 or 1


idontkno

Recommended Posts

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.

Link to comment
Share on other sites

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>';

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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!

Link to comment
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.