We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21417
    • 486 Posts
    Hello,

    For a MODX 1.0.6 site...

    I am hoping to show an alternate chunk if the date falls between a certain date range.

    The logic in the call might be something like:
    If the date (server date?) is between 15.12.12 and 28.12.12, show chunk1, else show chunk2
    (with chunk2 being the default state when outside that date range)

    I'd like to manually include the start and end dates in the call rather than via template variables so I can use site wide.

    If possible it would be great to exclude the year so it works year to year without modification, but that is only a minor consideration.

    Does anyone have any tricks to achieve this? Whether with phx or a snippet.

    Jako has a date range phx modifier but I am unsure how to use it.

    Any help greatly appreciated.

    Thanks in advance.
    [ed. note: nickf08 last edited this post 13 years, 10 months ago.]
      Web design Adelaide
      http://gocreate.com.au
      • 4041
      • 788 Posts
      This snippet should fit your requirements.
      1. Create a new snippet with the code below:
      <?php
      /*
      [!timed_content?
          &active_months =`12` 
          &active_days=`15,16,17`
          // &active_days=`all` 
          &default_chunk=`12pm_2pm_default` 
          &active_chunk=`12pm_2pm`
      !]
      
      * set &active_days=`all` for all days of the month
      */
      
      $default_chunk = isset( $default_chunk ) ? $default_chunk : '';
      $active_chunk = isset( $active_chunk ) ? $active_chunk : '';
      $active_months = isset( $active_months ) ? explode( ',', $active_months ) : ''; 
      $active_days = isset( $active_days ) ? explode( ',', $active_days ) : '';
      
      $time_details = getdate( $_SERVER['REQUEST_TIME'] + $modx->config['server_offset_time'] );
      $output ='';
      $month_ok;
      $day_ok;
      if( is_array( $active_months ) ){
          $month_ok = ( !in_array( $time_details['mon'], $active_months ) ? FALSE : TRUE );
      }
      if( is_array( $active_days ) ) {
          if( in_array( $time_details['mday'], $active_days ) || $active_days[0] =='all' ){
              $day_ok = TRUE;
          } 
      }
      $print_chunk = ( $month_ok && $day_ok ? $active_chunk : $default_chunk );
          
      $output .='{{'.$print_chunk.'}}';
      
      /* 
        getdate() debug 
        uncomment the following lines to print time info
      */
      //$details = print_r( $time_details, true );
      //$output .='<p>'.str_replace( '[', '<br>[', $details ).'</p>';
      
      return $output;
      ?>


      2. Using the example snippet call at the top of the snippet code, modify the snippet params to suit and add the snippet call wherever you want the chunk to appear.

      3. You can also just set all the default params in the snippet code and make a call like so:
      // snippet call
      [!timed_content!]
      
      // snippet params
      $default_chunk = isset( $default_chunk ) ? $default_chunk : '12pm_2pm_default';
      $active_chunk = isset( $active_chunk ) ? $active_chunk : '12pm_2pm';
      $active_months = isset( $active_months ) ? $active_months : '12';
      $active_days = isset( $active_days ) ? $active_days : '1,3,5,7,10';


      Hope that helps smiley
      [ed. note: breezer last edited this post 13 years, 10 months ago.]
        xforum
        http://frsbuilders.net (under construction) forum for evolution
        • 21417
        • 486 Posts
        Hi Breezer,

        You are a genius. That is absolutely brilliant. It returns exactly what I was hoping for and works like a charm in the testing I have done.

        Thank you so very much, I really appreciate it.

        All the best.
          Web design Adelaide
          http://gocreate.com.au
          • 21417
          • 486 Posts
          Hi Breezer,

          The snippet works great if I enter 11 in the active month - but if I enter 12 for December or any earlier months (1 or 2 or 3 etc... 10), no chunks are returned at all. The debug info is output no matter what month is entered.

          This works with 11:
          [!timedContent? &active_months =`11` &active_days=`15,16,17,18` &default_chunk=`content-enabled` &active_chunk=`content-disabled`!]


          This does not work with 12 (month being the only change):
          [!timedContent? &active_months =`12` &active_days=`15,16,17,18` &default_chunk=`content-enabled` &active_chunk=`content-disabled`!]


          Any ideas on what is happening?

          Thanks in advance.
            Web design Adelaide
            http://gocreate.com.au
            • 4041
            • 788 Posts
            I modified the code in my post above so give that a try, it works much better now smiley

            Also if you want to have the active chunk for all days of the month, make &active_days=`all`
              xforum
              http://frsbuilders.net (under construction) forum for evolution
              • 21417
              • 486 Posts
              Wow, thanks Breezer.

              That is excellent. It looks to work for any month now which is fantastic.

              I really like the "all" addition, that's very nice.

              Many thanks again for the tweak.

              Have a great day!
                Web design Adelaide
                http://gocreate.com.au