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 12-23-2006, 11:15 PM   PM User | #1
ACJavascript
Regular Coder

 
Join Date: Jun 2002
Location: FL, USA
Posts: 734
Thanks: 0
Thanked 0 Times in 0 Posts
ACJavascript is on a distinguished road
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
ACJavascript is offline   Reply With Quote
Old 12-24-2006, 02:19 AM   PM User | #2
CFMaBiSmAd
Senior Coder

 
CFMaBiSmAd's Avatar
 
Join Date: Oct 2006
Location: Denver, Colorado USA
Posts: 2,712
Thanks: 2
Thanked 251 Times in 243 Posts
CFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the rough
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.
CFMaBiSmAd is offline   Reply With Quote
Reply

Bookmarks

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 08:42 AM.


Advertisement
Log in to turn off these ads.