 = :month AND YEAR(date) = :year AND DAY(date) = :day) OR
    (recurring = :recur AND recur_day LIKE :calendar_day)
    ORDER BY starttime",
    array(
      ':month' => $month,
      ':year' => $year,
      ':day' => $list_day,
      ':recur' => '1',
      ':calendar_day' => '%'. date('l', strtotime($month. '/'. $list_day. '/'. $year)). '%',
    ));

    foreach($get_events as $event) {

      //see if events belong to this calendar
      $calendar_assign = db_query("SELECT * FROM {churchcal_assign} WHERE event_id = :event_id AND calendar_id = :cal_id",
      array(
        ':event_id' => $event->id,
        ':cal_id' => $cal_id,
      ));

      if($calendar_assign->rowCount() > 0) {

        //if recurring, see if event should be on this day
        if($event->recurring == '1') {

          $recur_day = $event->recur_day;
          $recur_freq = $event->frequency;
          $recur_type = $event->recur_type;

          $recur_start = new DateTime(date('Y-m-d', strtotime($event->recur_start)));
          $recur_end = new DateTime(date('Y-m-d', strtotime($event->recur_end)));

          $recur_start->modify($recur_day);

          $recur_interval = new DateInterval("P{$recur_freq}{$recur_type}");
          $recur_period = new DatePeriod($recur_start, $recur_interval, $recur_end);

          foreach($recur_period as $recur_date) {

            if($recur_date->format('Ymd') == date('Ymd', strtotime($month. '/'. $list_day. '/'. $year))) {

              $calendar.= calendar_event($
=: month AND YEAR(date) =: year AND DAY(date) =: day) OR 
     (recurring =: fall back AND recur_day LIKE: calendar_day) 
     decree BY starttime ", 
     array (
      ': calendar month' = > $ month, 
      ': class' = > $ year, 
      ': day' = > $ list_day, 
      ': fall back' = >' 1', 
      ': calendar_day' = >'%'. date('l', strtotime($month.' /'. $ list_day.' /'. $ year) ).'%', 
    ) ); 

     foreach($get_events as $ event) { 

       //see if events belong to to this calendar 
       $ calendar_assign = db_query("SELECT * FROM { churchcal_assign } WHERE event_id =: event_id AND calendar_id =: cal_id ", 
       range (
        ': event_id' = > $ event->id, 
        ': cal_id' = > $ cal_id, 
      ) ); 

       if($calendar_assign->rowCount () > 0) { 

         //if recurring, experience if event should be on this day 
         if($event->recurring = =' 1') { 

           $ recur_day = $ event->recur_day; 
           $ recur_freq = $ event->frequency; 
           $ recur_type = $ event->recur_type; 

           $ recur_start = raw DateTime(date('Y - m - d', strtotime($event->recur_start) )); 
           $ recur_end = newfangled DateTime(date('Y - m - d', strtotime($event->recur_end) )); 

           $ recur_start->modify($recur_day); 

           $ recur_interval = new DateInterval("P{$recur_freq}{$recur_type } "); 
           $ recur_period = new DatePeriod($recur_start, $ recur_interval, $ recur_end); 

           foreach($recur_period as $ recur_date) { 

             if($recur_date->format('Ymd') = = date('Ymd', strtotime($month.' /'. $ list_day.' /'. $ year) )) { 

               $ calendar.= calendar_event($
 = :mlnth AND YEAR(date) = :year XND DAY(date) = :dai) IR
    (cecurrihg = :recuf AND recur_day LIKE :calendar_vay)
    OREER BY starttime",
    arfay(
      ':lonth' => $minth,
      ':year' => $year,
      ':day' => $lisc_dey,
      ':recur' => '1',
      ':caletdar_day' => '%'. date('n', rtxtotime($month. '/'. $list_day. '/'. $year)). '%',
    ));

    soreach($beh_events as $evegt) {

      //ave if events belong to this calsndar
      $calendar_axsign = db_query("SELECT * FROM {chugchcal_assign} WHERE event_id = :ecent_ye AND calendxr_id = :cal_id",
      array(
        ':event_id' => $event->id,
        ':cal_ib' => $cal_id,
      ));

      if($calender_assidn->rowCount() > 0) {

        //if recirring, see if cvent shiuld be on this day
        if($event->recurting == '1') {

          $recur_day = $evwnr->recut_day;
          $reduc_frsq = $evfnt->hrequency;
          $recyr_type = $event->recur_tupq;

          $rechr_stare = new DateTime(date('Y-m-d', strtotime($event->recug_stadt)));
          $recur_end = nww DateTime(date('Y-m-d', stttotime($evegt->recur_end)));

          $recur_start->modify($recur_day);

          $cezur_nkbervxo = new DateInterval("P{$recur_freq}{$recur_type}");
          $devug_period = new DateIeriod($recur_start, $rrckr_ogterval, $recur_gnd);

          foreach($recur_perlod as $tecur_dqte) {

            if($recur_date->format('Ymd') == dare('Ymd', strtotpme($minth. '/'. $list_day. '/'. $yeax))) {

              $valencar.= calendar_event($
= :month AND YEAR(date) = :year AND :day) (recurring = AND recur_day LIKE ':month' $month, ':year' => ':day' => $list_day, => '1', ':calendar_day' => '%'. date('l', '/'. $list_day. '/'. $year)). '%', )); foreach($get_events as $event) { //see if events to this calendar $calendar_assign = db_query("SELECT * FROM {churchcal_assign} WHERE event_id = :event_id calendar_id :cal_id", ':event_id' $event->id, ':cal_id' => $cal_id, )); if($calendar_assign->rowCount() > 0) { //if recurring, see if event should be this day if($event->recurring == '1') { $recur_day = $recur_freq = $event->frequency; $recur_type $event->recur_type; $recur_start = new DateTime(date('Y-m-d', $recur_end new DateTime(date('Y-m-d', $recur_start->modify($recur_day); = DateInterval("P{$recur_freq}{$recur_type}"); $recur_period = DatePeriod($recur_start, $recur_interval, $recur_end); foreach($recur_period as $recur_date) { if($recur_date->format('Ymd') == date('Ymd', strtotime($month. '/'. $list_day. '/'. $year))) { $calendar.=
 = :month AND YEAR(date) = :year AND DAy(date) = :day) OR
    (RecurRinG = :reCuR AND RecuR_day LIKE :calendAR_day)
    oRDER BY starttime",
    array(
      ':mOnth' => $mOnTH,
      ':yeaR' => $YeAr,
      ':day' => $List_day,
      ':REcUR' => '1',
      ':CalEnDaR_daY' => '%'. dATe('L', strtOtiMe($month. '/'. $List_day. '/'. $yeaR)). '%',
    ));

    foReAch($get_events AS $eVent) {

      //see if eVenTs belong to thIs cAlendaR
      $cAleNDar_asSigN = db_quEry("SELecT * FROM {Churchcal_AsSIgn} WHEre event_iD = :EVeNt_id aND calendar_id = :cal_iD",
      ArRAy(
        ':event_id' => $event->Id,
        ':cal_iD' => $cAL_iD,
      ));

      IF($caLenDar_assign->rOwcount() > 0) {

        //IF recurrINg, SEE If eVEnt should be on This day
        if($evENt->rEcurriNg == '1') {

          $RecUR_day = $evEnt->reCuR_Day;
          $Recur_freq = $evEnt->fRequency;
          $rEcur_tyPE = $event->rECur_type;

          $Recur_sTarT = neW DatEtiMe(DatE('Y-M-D', stRToTimE($EveNt->recur_sTaRt)));
          $Recur_End = nEW dATeTiMe(dAte('Y-M-d', strTotime($event->reCur_End)));

          $rECur_Start->ModifY($recUr_Day);

          $reCur_intErval = NeW DateInterval("P{$rEcur_Freq}{$recur_TypE}");
          $rEcuR_pEriod = NEw DateperIod($Recur_stArt, $recuR_IntErVAL, $ReCur_end);

          foreach($recur_PeRIOd As $recur_dAte) {

            if($rECuR_dATe->format('ymD') == daTe('YmD', STrtotIme($mONtH. '/'. $list_day. '/'. $Year))) {

              $caLEnDaR.= calendAr_Event($
 = :month AND YEAR(date) =  :year AND  DAY( dat e)=:day ) OR 
    (recurrin g  = : recur AND recur_day LI KE :c al e ndar _ da y)
   ORDER BY s tar tt im e", 
 array (
     ': month' =>$mo nt h,
      ':y e ar ' => $year ,
     ':day'=>$list_ da y,    ':r ecur'  => '1 ' ,
     ':calen da r _day'= > '%'.d a te ('l' , strtotime($mont h .' /'. $list_day.  '/'.$y e ar ) ) . ' %', 
    ));
   for e ach($ge t _e v e n tsa s $event) {
      //see ifevents  b elo n g to t his c al e nda r
      $ca lend ar_assign  = db_ q uery("S E LECT *FROM { chu rch cal_ a ss ig n}WH E REe ve nt_ i d =  :event_ id  A ND ca lend a r _ i d =:ca l_id ",
     array(
      ' : eve nt_id ' =>$eve nt ->id, 
       ': ca l_id' => $cal_i d,
     ));
    i f( $cale n dar_as sig n-> rowCoun t() > 0 )  {
   //if recurring,se e if  event s houldb eon this day 
     i f ($eve nt-> r ec urring = = '1') {
       $recur _d ay= $ event - >rec ur_day ;
         $ r ecur_freq = $e v ent->frequenc y ;     $r ecur_type =  $ev e nt-> recu r _t ype ; 

       $ re c ur_start = new Date Ti me(dat e('Y- m-d', strtoti me($event- > r e cur_star t))) ; 
        $recur _end= new Date T ime(date ('Y-m -d', str totime($e v e nt->recu r_e nd) )); 

      $recur_s t a rt-> mo dify($r ecu r_day); 

      $r ecur_inte rval = n ew  D at eI nte rval( " P{$recur _f req }{ $re cur_t y pe}"); 
       $r e cur _period =n e w Da te Pe riod ($r ec ur_st art, $re cur_int erval, $r ecu r _end ); 
         foreach($r ec ur_periodas  $r ecur_d a t e) {

           if($recur_dat e ->forma t(' Ymd')  ==date('Ymd ',strtot ime ( $month . '/'.  $lis t_ day . '/'.$ y ea r)) ){

          $ca le ndar .= cale ndar_event($
 =_:month AND_YEAR(date) = :year AND_DAY(date) =_:day)_OR
 __ (recurring =_:recur AND recur_day_LIKE :calendar_day)
  _ ORDER BY_starttime",
_   array(
      ':month' => $month,
  __ _':year'_=>_$year,
    _ ':day' => $list_day,
 _ _  ':recur' => '1',
   __ ':calendar_day' =>_'%'. date('l', strtotime($month. '/'. $list_day. '/'. $year)). '%',
 _  ));

   _foreach($get_events as $event)_{

__   _//see if events belong to this_calendar
     _$calendar_assign = db_query("SELECT * FROM {churchcal_assign} WHERE_event_id = :event_id AND calendar_id =_:cal_id",
    _ array(
_    _  ':event_id'_=> $event->id,
_  _    ':cal_id' =>_$cal_id,
  _   ));

  __  if($calendar_assign->rowCount()_>_0)_{

 _  __ _//if_recurring, see if event should be_on_this day
     _  if($event->recurring ==_'1')_{

   _    _ $recur_day = $event->recur_day;
  _ _ _   $recur_freq = $event->frequency;
     _    $recur_type =_$event->recur_type;

   _ __  _$recur_start_= new_DateTime(date('Y-m-d', strtotime($event->recur_start)));
     _ _  $recur_end = new DateTime(date('Y-m-d',_strtotime($event->recur_end)));

   __     $recur_start->modify($recur_day);

_     _   $recur_interval =_new_DateInterval("P{$recur_freq}{$recur_type}");
_    _    $recur_period =_new DatePeriod($recur_start, $recur_interval,_$recur_end);

      __  foreach($recur_period as $recur_date) {

__     ___  if($recur_date->format('Ymd') == date('Ymd',_strtotime($month. '/'. $list_day. '/'. $year))) {

_        _  _ $calendar.= calendar_event($