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

    I'm currently using Modx Revolution 2.3.1 and have a tp which inserts the word "New". I'd like to remove this tv from the page after 2 weeks from the last publish or created date.

    I have no idea where to start so any suggestions would be very welcome.

    Thanks in advance.

    jvollands

    This question has been answered by jvollands. See the first response.

      • 4172
      • 5,888 Posts
      try this:

      new snippet 'getFormatedDateTime'

      //[[!getFormatedDateTime]]
      //[[!getFormatedDateTime? &timestring=`+1 day`]] see: http://php.net/manual/de/function.strtotime.php
      //[[!getFormatedDateTime? &timestring=`+1 day` &format=`%Y-%m-%d`]]
      
      $time = $modx->getOption('time',$scriptProperties,time());
      $timestring = $modx->getOption('timestring',$scriptProperties,strftime('%Y-%m-%d %H:%M:%S'));
      $format = $modx->getOption('format',$scriptProperties,'%Y-%m-%d %H:%M:%S');
      
      
      $time = strtotime($timestring,$time);
      return strftime($format,$time);
      


      And with the If - snippet installed, call it like that:

      [[!If?
      &subject=`[[getFormatedDateTime? &time=`[[*publishedon]]`]]`
      &operand=`[[!getFormatedDateTime? &timestring=`+14 day`]]`
      &operator=`<`
      &then=`New`
      ]]
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        Personally, I wouldn't use a TV at all. I'd use a snippet, and output "New" as long as the resource's pub_date was less than or equal to two weeks before today.
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 15130
          • 4 Posts
          Quote from: Bruno17 at Oct 01, 2014, 06:36 AM
          try this:

          new snippet 'getFormatedDateTime'

          //[[!getFormatedDateTime]]
          //[[!getFormatedDateTime? ×tring=`+1 day`]] see: http://php.net/manual/de/function.strtotime.php
          //[[!getFormatedDateTime? ×tring=`+1 day` &format=`%Y-%m-%d`]]
          
          $time = $modx->getOption('time',$scriptProperties,time());
          $timestring = $modx->getOption('timestring',$scriptProperties,strftime('%Y-%m-%d %H:%M:%S'));
          $format = $modx->getOption('format',$scriptProperties,'%Y-%m-%d %H:%M:%S');
          
          
          $time = strtotime($timestring,$time);
          return strftime($format,$time);
          


          And with the If - snippet installed, call it like that:

          [[!If?
          &subject=`[[getFormatedDateTime? &time=`[[*publishedon]]`]]`
          &operand=`[[!getFormatedDateTime? ×tring=`+14 day`]]`
          &operator=`<`
          &then=`New`
          ]]

          Thanks Bruno for the quick response.

          I tried to implement but I just couldn't work it out. I should have said I'm a web designer so any not as good as you dev guys.

          any pointers would be great,

          Thanks
            • 15130
            • 4 Posts
            Quote from: sottwell at Oct 01, 2014, 06:41 AM
            Personally, I wouldn't use a TV at all. I'd use a snippet, and output "New" as long as the resource's pub_date was less than or equal to two weeks before today.

            This does sounds interesting. If you have any examples to help get me started that would be great. I'm a web designer so please go easy with the code.

            thanks again.
              • 4172
              • 5,888 Posts
              this really should work.

              you just need to create a new snippet with that code.
              Install the If extra and put that If - snippet-call to your page.

              That's it.
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 3749
                • 24,544 Posts
                I believe pub_date is set to 0 when the Resource is published. I think you want the publishedon field.

                Using Susan's suggestion, you'd just create a snippet called New containing the code below and put this tag where you want the word 'New' to appear:

                [[!New]]


                <?php
                /* New snippet */
                
                /* Get the publishedon date */
                $publishedOn = $modx->resource->get('publishedon');
                
                /* This will be returned if it's not published or
                   more than two weeks old */
                $output = '';
                
                /* Do nothing if the resource is not published */
                if (!empty($publishedOn)) {
                    /* It's published -- convert the pub date to a timestamp */
                    $pTime = strtotime($publishedOn); 
                
                    /* See if it's less than two weeks old */
                    if ( ($pTime + 1209600) > time()) {
                        /* It is. Set the output to 'New' */
                        $output = 'New';
                        /* or in case you want to style it -- */
                        /* $output = '<span style="new_resource">New</span>'; */
                    }  
                }
                
                return $output;
                


                I do like Bruno17's method too, though the timestring property looks funky on my machine -- maybe the forum mangled it. My method might be easier to understand, and I think may involve less computation.

                If this works, you can remove the TV.
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting
                • discuss.answer
                  • 15130
                  • 4 Posts
                  Quote from: BobRay at Oct 02, 2014, 12:22 AM
                  I believe pub_date is set to 0 when the Resource is published. I think you want the publishedon field.

                  Using Susan's suggestion, you'd just create a snippet called New containing the code below and put this tag where you want the word 'New' to appear:

                  [[!New]]


                  <!--?php
                  /* New snippet */
                  
                  /* Get the publishedon date */
                  $publishedOn = $modx--->resource->get('publishedon');
                  
                  /* This will be returned if it's not published or
                     more than two weeks old */
                  $output = '';
                  
                  /* Do nothing if the resource is not published */
                  if (!empty($publishedOn)) {
                      /* It's published -- convert the pub date to a timestamp */
                      $pTime = strtotime($publishedOn); 
                  
                      /* See if it's less than two weeks old */
                      if ( ($pTime + 1209600) > time()) {
                          /* It is. Set the output to 'New' */
                          $output = 'New';
                          /* or in case you want to style it -- */
                          /* $output = '<span style="new_resource">New</span>'; */
                      }  
                  }
                  
                  return $output;
                  


                  I do like Bruno17's method too, though the timestring property looks funky on my machine -- maybe the forum mangled it. My method might be easier to understand, and I think may involve less computation.

                  If this works, you can remove the TV.

                  Bob thanks this is amazing worked a treat, you're a legend.

                  http://jamievollands.com/matthews-mee/index.php?id=23

                  Just need to test now so will change to an hour or so.

                  Thanks again



                    • 3749
                    • 24,544 Posts
                    I'm glad I could help. Thanks for reporting back. smiley
                      Did I help you? Buy me a beer
                      Get my Book: MODX:The Official Guide
                      MODX info for everyone: http://bobsguides.com/modx.html
                      My MODX Extras
                      Bob's Guides is now hosted at A2 MODX Hosting