Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-30-2009, 12:24 AM   PM User | #1
nojo191
New Coder

 
Join Date: Jul 2007
Location: Kansas
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
nojo191 is an unknown quantity at this point
Alternating day 1/2 counter

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!
__________________
~Nojo191
Noah Benham's website
nojo191 is offline   Reply With Quote
Old 04-30-2009, 01:07 AM   PM User | #2
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
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
oesxyl is offline   Reply With Quote
Old 04-30-2009, 05:31 AM   PM User | #3
Len Whistler
Senior Coder

 
Len Whistler's Avatar
 
Join Date: Jul 2002
Location: Vancouver, BC Canada
Posts: 1,323
Thanks: 26
Thanked 100 Times in 100 Posts
Len Whistler is on a distinguished road
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 Code:
<?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";

?>
__________________
Leonard Whistler

Last edited by Len Whistler; 04-30-2009 at 05:37 AM..
Len Whistler is offline   Reply With Quote
Old 04-30-2009, 09:09 PM   PM User | #4
nojo191
New Coder

 
Join Date: Jul 2007
Location: Kansas
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
nojo191 is an unknown quantity at this point
Looks good but does it start over every week? Changing the 1 to a 2 doesn't have an affect... I'm php illiterate.
__________________
~Nojo191
Noah Benham's website
nojo191 is offline   Reply With Quote
Old 04-30-2009, 09:21 PM   PM User | #5
Len Whistler
Senior Coder

 
Len Whistler's Avatar
 
Join Date: Jul 2002
Location: Vancouver, BC Canada
Posts: 1,323
Thanks: 26
Thanked 100 Times in 100 Posts
Len Whistler is on a distinguished road
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.


----
__________________
Leonard Whistler

Last edited by Len Whistler; 04-30-2009 at 09:25 PM..
Len Whistler is offline   Reply With Quote
Old 04-30-2009, 11:05 PM   PM User | #6
Len Whistler
Senior Coder

 
Len Whistler's Avatar
 
Join Date: Jul 2002
Location: Vancouver, BC Canada
Posts: 1,323
Thanks: 26
Thanked 100 Times in 100 Posts
Len Whistler is on a distinguished road
Not fully tested.


PHP Code:
<?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";

?>
__________________
Leonard Whistler

Last edited by Len Whistler; 04-30-2009 at 11:48 PM..
Len Whistler is offline   Reply With Quote
Users who have thanked Len Whistler for this post:
nojo191 (05-01-2009)
Old 05-01-2009, 02:07 AM   PM User | #7
nojo191
New Coder

 
Join Date: Jul 2007
Location: Kansas
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
nojo191 is an unknown quantity at this point
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!!!
__________________
~Nojo191
Noah Benham's website
nojo191 is offline   Reply With Quote
Old 05-01-2009, 02:15 AM   PM User | #8
Len Whistler
Senior Coder

 
Len Whistler's Avatar
 
Join Date: Jul 2002
Location: Vancouver, BC Canada
Posts: 1,323
Thanks: 26
Thanked 100 Times in 100 Posts
Len Whistler is on a distinguished road
Quote:
Originally Posted by nojo191 View 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.

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.


-------
__________________
Leonard Whistler
Len Whistler is offline   Reply With Quote
Old 05-01-2009, 06:06 AM   PM User | #9
nojo191
New Coder

 
Join Date: Jul 2007
Location: Kansas
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
nojo191 is an unknown quantity at this point
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).

  
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
Noah Benham's website

Last edited by nojo191; 05-01-2009 at 08:47 AM.. Reason: updated code for friday
nojo191 is offline   Reply With Quote
Old 05-01-2009, 06:09 AM   PM User | #10
nojo191
New Coder

 
Join Date: Jul 2007
Location: Kansas
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
nojo191 is an unknown quantity at this point
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...

The external day.txt file is currently set to 0.
__________________
~Nojo191
Noah Benham's website

Last edited by nojo191; 05-01-2009 at 06:10 AM.. Reason: add external day.txt file note
nojo191 is offline   Reply With Quote
Old 05-01-2009, 06:19 AM   PM User | #11
Len Whistler
Senior Coder

 
Len Whistler's Avatar
 
Join Date: Jul 2002
Location: Vancouver, BC Canada
Posts: 1,323
Thanks: 26
Thanked 100 Times in 100 Posts
Len Whistler is on a distinguished road
Try:

PHP Code:
$result = ($day_of_year 2) + $adjustment// output 1 or 2. 
Or change adjustment values in txt file to 1 or 2, not 0.


-----
__________________
Leonard Whistler

Last edited by Len Whistler; 05-01-2009 at 06:22 AM..
Len Whistler is offline   Reply With Quote
Old 05-01-2009, 08:46 AM   PM User | #12
nojo191
New Coder

 
Join Date: Jul 2007
Location: Kansas
Posts: 72
Thanks: 3
Thanked 0 Times in 0 Posts
nojo191 is an unknown quantity at this point
Looks good, I'll take a second look after the weekend.

Code updated in post #9 for friday. Day.txt shows "2".
__________________
~Nojo191
Noah Benham's website
nojo191 is offline   Reply With Quote
Reply

Bookmarks

Tags
alternating, counter, day, school

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:11 AM.


Advertisement
Log in to turn off these ads.