...

View Full Version : Alternating day 1/2 counter



nojo191
04-30-2009, 12:24 AM
My school has day 1s and day 2s, which alternate every day. I'm trying to print this with php. For example, if monday is a day 1, tuesday a day 2, etc, the following monday would be a day 2 because there are 5 days in a week. I tried this with javascript and realized this would all be client side so the users wouldn't see the same thing.

I also need a method of resetting the date for things such as snow days or days we don't have school. On snow days we just skip over that day like we do a weekend. So if Tuesday is a day 1, and don't have school Wednesday, Thursday would be a day 2. I know I'm asking for a lot here but I truly don't have any idea where to start here. I should probably post this in the web projects forum to pay someone but I'm only asking for help, not for someone to complete the entire thing.

To recap: I need suggestions/code for a script that prints something like "Today is a day 1. Tomorrow is a day 2." or "Today is a Sunday. Tomorrow is a day 1." and remembers what day it actually is. I also need a way to reset the count to either day 1 or day 2 and have it continue from this day on (this should not be seen on the same page as the day is printed).

I know I'm asking for a lot but thanks for help/suggestions!

oesxyl
04-30-2009, 01:07 AM
you can preserving state by saving/loading something in/from a file.
because php script is executed only when you request that page you must check using date probably if you must compute or not next state.
how you compute and how and what you print I guess is not a problem.
this will help you to write the code:

http://www.php.net/manual/en/

best regards

Len Whistler
04-30-2009, 05:31 AM
This should work, just change $set to 1 or 2. I haven't fully tested it though and it will need some small adjustments to get all the output you want.


<?php

$set = "1"; // 1 or 2

$day_of_week = date('N'); // 1 (for Monday) through 7 (for Sunday).


$result = ($day_of_week % $set) + 1; // output 1 or 2. Study Modulus.

switch ($day_of_week) {

case '1':
$output = "Today is day $result";
break;
case '2':
$output = "Today is day $result";
break;
case '3':
$output = "Today is day $result";
break;
case '4':
$output = "Today is day $result";
break;
case '5':
$output = "Today is day $result";
break;
case '6':
$output = "Today is Saturday";
break;
default:
$output = "Today is Sunday";
break;

}

echo "$output";

?>

nojo191
04-30-2009, 09:09 PM
Looks good but does it start over every week? Changing the 1 to a 2 doesn't have an affect... :( I'm php illiterate.

Len Whistler
04-30-2009, 09:21 PM
Ok ....I see some areas which need adjustment.

date('z') should be used to output 1 -365 for each day of the year and then use the modulus operator to change that to 1 or 2. Then a statement would be required to override date('z') for Sundays and Saturdays. Changing 1 to a 2 should be working on the example I gave above.

Later on tonight I will make some changes when I'm on my home computer.


----

Len Whistler
04-30-2009, 11:05 PM
Not fully tested.



<?php

$adjustment = "1"; // change to 1 or 0.

$day_of_year = date('z'); // 1 through 365.
$result = ($day_of_year % 2) + $adjustment; // output 1 or 2.
$day_of_week = date('N'); // 1 (for Monday) through 7 (for Sunday).

if ($result == "1") {
$next_day = "2";
} else {
$next_day = "1";
}

$output = "Today is day $result. Tomorrow is day $next_day";

if ($day_of_week == "6") {
$output = "Today is Saturday. Tomorrow is Sunday";
}
if ($day_of_week == "7") {
$output = "Today is Sunday. Tomorrow is day $next_day";
}

echo "$output";

?>

nojo191
05-01-2009, 02:07 AM
Looks perfect. I'll let you know if I have any problems but this looks complete to me. The only thing else would be an easier way to change the adjustment but I'll figure it out, that's easy.

Thank you!!!

Len Whistler
05-01-2009, 02:15 AM
Looks perfect. I'll let you know if I have any problems but this looks complete to me. The only thing else would be an easier way to change the adjustment but I'll figure it out, that's easy.

Thank you!!!

I recommend you study the Modulus operator because I think my math might be wrong on this line: $result = ($day_of_year % 2) + $adjustment;

The $adjustment = "1"; // change to 1 or 0. can go anywhere in your webpage that's more convenient to change. You can also login to your gmail account to switch between day 1 and 2, Google Docs - in this case the spreadsheet - can be used to manipulate data on web pages.


-------

nojo191
05-01-2009, 06:06 AM
I posted the widget to my school's site with the notice it's in beta mode.

You can see your much appreciated handiwork at http://tinyurl.com/ceh4k4. I'm now calling the 0 or 1 adjustment through an internal file named "day.txt" which contains only that one number. This makes editing it pretty much foolproof for the non coders that will be moderating in the future.

New code:

<?php

$adjustment = file_get_contents ('day.txt'); // change to 1 or 2 in day.txt file.

$day_of_year = date('z'); // 1 through 365.
$result = ($day_of_year % 2) + $adjustment; // output 1 or 2.
$day_of_week = date('N'); // 1 (for Monday) through 7 (for Sunday).

if ($result == "1") {
$next_day = "2";
} else {
$next_day = "1";
}

$output = "Today:<br /><div style=\"font-size:16px; font-weight:bold;\">$result</div>Tomorrow:<br /><div style=\"font-size:16px; font-weight:bold;\">$next_day</div>";

if ($day_of_week == "5") {
$output = "Today:<br /><div style=\"font-size:16px; font-weight:bold;\">$result</div>Tomorrow:<br />Saturday";
}
if ($day_of_week == "6") {
$output = "Today:<br />Saturday<br />Tomorrow:<br />Sunday";
}
if ($day_of_week == "7") {
$output = "Today:<br />Sunday<br />Tomorrow:<br /><div style=\"font-size:16px; font-weight:bold;\">$next_day</div>";
}

?>

nojo191
05-01-2009, 06:09 AM
Yep, just noticed an error. I'm now seeing that today is a day 0 and tomorrow is a day 1 as midnight passes. Everything's setup and live but I'm not quite sure how to fix this... :confused:

The external day.txt file is currently set to 0.

Len Whistler
05-01-2009, 06:19 AM
Try:


$result = ($day_of_year % 2) + 1 + $adjustment; // output 1 or 2.

Or change adjustment values in txt file to 1 or 2, not 0.


-----

nojo191
05-01-2009, 08:46 AM
Looks good, I'll take a second look after the weekend.

Code updated in post #9 for friday. Day.txt shows "2".



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum