By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,852 Members | 2,152 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,852 IT Pros & Developers. It's quick & easy.

weekstart dates of a year

omerbutt
100+
P: 638
hi,
i am working on a reservations component in joomla 1.6, i have to provide the client with a form where he can enter the door codes for the Lodges , the door code changes on every monday and remains the same for the whole week , the table that holds the door codes,is as follows.

Expand|Select|Wrap|Line Numbers
  1. id
  2. code
  3. date_start
  4. date_end
  5.  
the date_start holds the starting day's date for the week i.e Monday obviously and the date_end holds the last day's date of the week i.e Sunday.

Now in the add new door code i have to provide the client with a drop-down that contains the list of the dates of every weeks Starting date up to 3 year in the future.
Any idea how to calculate the total mondays in a year as i need to loop and the loop needs to run up to Total mondays in the selected year,
Regards,
Omer Aslam
Nov 16 '11 #1

✓ answered by omerbutt

i changed it i provided the client with the drop down to select the year and then the month and after that i populated the drop down of all the dates for mondays in that particular month and year through ajax.
here is the core code for the weeks selection (Note: Joomla 1.6 component development)
Expand|Select|Wrap|Line Numbers
  1. public function getWeeks(){
  2.         $month    =    JRequest::getVar('month');
  3.         $year    =    JRequest::getVar('year');
  4.         $total_weeks    =    6;
  5.  
  6.         $start_date    =    new DateTime(date('Y-m-d',strtotime('first monday of '.$year.'-'.$month.'-01'))) ;
  7.         $end_date    =    new DateTime(date_format($start_date,'Y-m-d'));
  8.  
  9.         echo '<select name="weeks" id="weeks" onchange="putVal(this.value,\'date_start\');" class="required">';
  10.         echo '<option value="none">Select a Monday</option>';    
  11.         for($w=1; $w<=$total_weeks; $w++){
  12.             echo '<option value="'.date_format($end_date,'Y-m-d').'">'.date_format($end_date,'Y-m-d').'</option>';    
  13.             date_add($end_date, date_interval_create_from_date_string('7 days'));
  14.             if(date_format($end_date,'m')!=date_format($start_date,'m')){
  15.                 break;
  16.             }
  17.         }
  18.         echo '</select>';
  19.     }
  20.  
regards,
Omer Aslam

Share this Question
Share on Google+
2 Replies


zorgi
Expert 100+
P: 431
I would use time stamp for start date to make all calculations. Something like this:
Expand|Select|Wrap|Line Numbers
  1. $last_monday = strtotime("last Monday");
  2. //7 days * 24 hours * 60 minutes * 60 seconds = 604800 seconds
  3. $next_monday = $last_monday + 604800;
Nov 16 '11 #2

omerbutt
100+
P: 638
i changed it i provided the client with the drop down to select the year and then the month and after that i populated the drop down of all the dates for mondays in that particular month and year through ajax.
here is the core code for the weeks selection (Note: Joomla 1.6 component development)
Expand|Select|Wrap|Line Numbers
  1. public function getWeeks(){
  2.         $month    =    JRequest::getVar('month');
  3.         $year    =    JRequest::getVar('year');
  4.         $total_weeks    =    6;
  5.  
  6.         $start_date    =    new DateTime(date('Y-m-d',strtotime('first monday of '.$year.'-'.$month.'-01'))) ;
  7.         $end_date    =    new DateTime(date_format($start_date,'Y-m-d'));
  8.  
  9.         echo '<select name="weeks" id="weeks" onchange="putVal(this.value,\'date_start\');" class="required">';
  10.         echo '<option value="none">Select a Monday</option>';    
  11.         for($w=1; $w<=$total_weeks; $w++){
  12.             echo '<option value="'.date_format($end_date,'Y-m-d').'">'.date_format($end_date,'Y-m-d').'</option>';    
  13.             date_add($end_date, date_interval_create_from_date_string('7 days'));
  14.             if(date_format($end_date,'m')!=date_format($start_date,'m')){
  15.                 break;
  16.             }
  17.         }
  18.         echo '</select>';
  19.     }
  20.  
regards,
Omer Aslam
Nov 21 '11 #3

Post your reply

Sign in to post your reply or Sign up for a free account.