I see your point, but Robert Jackson already had a starting date. My problem was finding a date within the specified week..
Here are some code working snippets that solves my problem.
Thanks to you Vincent who pointed me in the right direction, Robert Jackson and Allan Kent for writing the code I have ripped and adapted.
<?
function dateAdd ($interval, $number, $date) {
/* The DateAdd function, according the VBScript documentation I have, "Returns a date to which a specified time interval has been added."
The syntax is DateAdd(interval,number,date)
The interval is a string expression that defines the interval you want to add. For example minutes or days,
the number is the number of that interval that you wish to add, and the date is the date.
Interval can be one of:
yyyy year
q Quarter
m Month
y Day of year
d Day
w Weekday
ww Week of year
h Hour
n Minute
s Second
*/
$date_time_array = getdate($date);
$hours = $date_time_array["hours"];
$minutes = $date_time_array["minutes"];
$seconds = $date_time_array["seconds"];
$month = $date_time_array["mon"];
$day = $date_time_array["mday"];
$year = $date_time_array["year"];
switch ($interval) {
case "yyyy":
$year +=$number;
break;
case "q":
$year +=($number*3);
break;
case "m":
$month +=$number;
break;
case "y":
case "d":
case "w":
$day+=$number;
break;
case "ww":
$day+=($number*7);
break;
case "h":
$hours+=$number;
break;
case "n":
$minutes+=$number;
break;
case "s":
$seconds+=$number;
break;
}
$timestamp = mktime($hours ,$minutes, $seconds,$month ,$day, $year);
return $timestamp;
}
?>
<head>
<title></title>
</head>
<body>
<?
$year = 2004;
$week = 46;
$dweek = $week - 1;
echo "a day in week $week, year $year is : ".date("D d M Y",dateAdd("ww",$dweek, mktime (0, 0, 0, 1,1, $year) ))."<br>";
$month = date("m",dateAdd("ww",$dweek, mktime (0, 0, 0, 1,1, $year) ));
$day = date("d",dateAdd("ww",$dweek, mktime (0, 0, 0, 1,1, $year) ));
echo "base date: ".$year."-".$month."-".$day."<br>";
echo "last monday: ".date("Y-m-d", mktime(0,0,0,$month,$day+1-date("w",mktime(0,0,0,$month,$day,$year)),$year))."<br>";
echo "next sunday: ".date("Y-m-d", mktime(0,0,0,$month,$day+7-date("w",mktime(0,0,0,$month,$day,$year)),$year))."<br>";
?>
</body>