Jump to content

get date from weekday and weeknumber.


l_kris06

Recommended Posts

Hi all,

 

I am generating a recurring instance of an .ics for outlook recurring meeting.

I am stuck in a case where i need to get the next date from a week number and a weekday.

 

for instance,

 

curDate = 22/12/2008

weekDay = Wednesday

weekNumber = 5

 

I need 31/12/2008 as return value.

 

can somebody help? i think i am stuck.

 

Rgds,

Kris

 

Link to comment
Share on other sites

Hi Mark,

 

  The significance of the weeknumber in this case the value 5 is that

 

I basically want the date of the 5th week on which the day is wednesday.

 

Another example:

 

curDate = 22/12/2008

weekDay = Friday

weekNumber = 3

 

I would expect 19/12/2008

 

 

This is confusing, I dont knw how to go about it. Any help would be greatly appreciated.

 

Best,

Kris

 

 

 

 

 

Link to comment
Share on other sites

I basically want the date of the 5th week on which the day is wednesday.

The fifth week from what?

 

I can only guess that you mean the start of the month 19th December 2008 was the 3rd Friday in the month

31st December is the 5th Wednesday in the month

 

Is this correct?

Link to comment
Share on other sites

Based on that guess:

$daysArray = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');

$currentDate = time();
$weekDay = 'Wednesday';
$weekNumber = 5;

$tmpWeekNbr = $weekNumber;
switch ($tmpWeekNbr) {
case 1 : $sup = 'st';
		 break;
case 2 : $sup = 'nd';
		 break;
case 3 : $sup = 'rd';
		 break;
default : $sup = 'th';
}

echo 'Current Date is '.date('d-M-Y',$currentDate).'<br />';
$dueDate = $baseDate = strtotime(date('Y-m',$currentDate).'-01');
echo 'Base Date is '.date('d-M-Y',$baseDate).'<br />';
$baseDoW = date('w',$baseDate);
$requiredDoW = $daysArray[$weekDay];
echo 'Month starts on a '.$daysArray[$baseDoW].'<br />';
echo 'Day required is '.$weekDay.'<br />';
echo 'Week required is '.$weekNumber.'<br />';;


if ($requiredDoW == $baseDoW) {
$weekNumber--;
}
while ($weekNumber > 0) {
$dueDate = strtotime('next '.$weekDay,$dueDate);
$weekNumber--;
}

echo $tmpWeekNbr.$sup.' '.$weekDay.' in '.date('F',$dueDate).' is the '.date('d-M-Y',$dueDate).'<br />';

Based on this, you should be able to calculate for any month/year rather than just for the current month, and add some validation to the Day of Week and Week number so that the result doesn't spill over into the following month.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.