View Full Version : Date and Time issues

12-24-2006, 12:15 AM
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?


12-24-2006, 03:19 AM
Here is some code to do this (tested) -

$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(0, 0, 0, date("m") , date("d")+$difference, date("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.