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 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    FL, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Date and Time issues

    Hey all,

    Okay, I have a problem -
    I have links from MONDAY to FRIDAY... and when the user clicks on the link it will send the script a value of 1-5

    1 through 5 equals mon,tue,wed,thr,fri...

    Now my problem is I need to take that 1-5 and convert it to the DAY its on.

    Meaning, today is Saturday(23rd) if they click Monday(Happends to be xmas) it woudl show the date 25th.

    Anybody know how I can do this? or a better way to do it?

    CYWebmaster.com - See why we dot com!!
    ACJavascripts.com - Cut & Paste Javascripts!
    SimplyProgram.com - Personal Blog

  2. #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,870
    Thanks
    3
    Thanked 454 Times in 444 Posts
    Here is some code to do this (tested) -
    PHP Code:
    <?php
    $today_day_number 
    date("w");

    echo 
    "Today's day of the week is: $today_day_number (0=Sun, 1=Mon...)";
    echo 
    "<br />";

    // test loop for possible values 1-5
    // $test_value is simulating the value received as input from the form

    for($test_value=1$test_value <=5;$test_value++){

        
    // if today_day_number is 6 (sat) - make this -1 so difference math works
        
    if($today_day_number == 6)
        {
        
    $today_day_number = -1;
        }

        
    // if test value (1-5) is greater than or equal to today_day_number, then test value is today or higher (coming week or current week)
        // if test value (1-5) is less than today_day_number, day entered has already gone past and is actually the following week (add 7)

        
    if($test_value >= $today_day_number)
        {
        
    $difference $test_value-$today_day_number;
        } else {
        
    $difference $test_value-$today_day_number+7;
        }
        echo 
    "Test value: $test_value, Difference: $difference, date: ";
        echo 
    date("d",mktime(000date("m")  , date("d")+$differencedate("Y"))); // mktime correctly does the math and wraps to the next week/month/year as needed
        
    echo "<br />";
    // end of test loop for possible values 1-5
    ?>
    I actually tested this within another loop to simulate all the possible values for $today_day_number (0-6) and it looks like it works correctly.
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.


 

Posting Permissions

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