Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Jul 2007
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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!

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    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

  • #3
    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
    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";

    ?>
    Last edited by Len Whistler; 04-30-2009 at 05:37 AM.
    Leonard Whistler

  • #4
    New Coder
    Join Date
    Jul 2007
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Looks good but does it start over every week? Changing the 1 to a 2 doesn't have an affect... I'm php illiterate.

  • #5
    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
    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.


    ----
    Last edited by Len Whistler; 04-30-2009 at 09:25 PM.
    Leonard Whistler

  • #6
    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
    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";

    ?>
    Last edited by Len Whistler; 04-30-2009 at 11:48 PM.
    Leonard Whistler

  • Users who have thanked Len Whistler for this post:

    nojo191 (05-01-2009)

  • #7
    New Coder
    Join Date
    Jul 2007
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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!!!

  • #8
    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
    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

  • #9
    New Coder
    Join Date
    Jul 2007
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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>";
      }

    ?>
    Last edited by nojo191; 05-01-2009 at 08:47 AM. Reason: updated code for friday

  • #10
    New Coder
    Join Date
    Jul 2007
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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.
    Last edited by nojo191; 05-01-2009 at 06:10 AM. Reason: add external day.txt file note

  • #11
    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
    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.


    -----
    Last edited by Len Whistler; 05-01-2009 at 06:22 AM.
    Leonard Whistler

  • #12
    New Coder
    Join Date
    Jul 2007
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Looks good, I'll take a second look after the weekend.

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


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •