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!
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:
$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";
?>
__________________
Leonard Whistler
Last edited by Len Whistler; 04-30-2009 at 05:37 AM..
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.
----
__________________
Leonard Whistler
Last edited by Len Whistler; 04-30-2009 at 09:25 PM..
$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";
?>
__________________
Leonard Whistler
Last edited by Len Whistler; 04-30-2009 at 11:48 PM..
Users who have thanked Len Whistler for this post:
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.
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.
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 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).
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...