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
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,036
    Thanks
    2
    Thanked 316 Times in 308 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.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

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