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

    Is it possible to calculate the difference between the creation date of a document and the current date? What i want to do is something like this :

    [+phx:if=`[*Date_Diff*]`:el=`3`:then=`{{new_chunk}}`+]

    meaning if a document is 3 days old (or less) to show the new_chunk

    Thank is advance
    Dimitris
      • 4172
      • 5,888 Posts
      you can do it with phx, but its a bit tricky and complicated. I would create a little snippet.
      this is not tested, but something like that should do it:

      <?php
      //[!newOrOldDoc? &days=`3` &newchunk=`new_chunk` &oldchunk=`old_chunk`!]
      
      $days=isset($days)?$days:1;
      return time()-$modx->documentObject['createdon']<=86400*$days?'{{'.$newchunk.'}}':'{{'.$oldchunk.'}}';


      or if you want to use it in Ditto something like that:

      <?php
      //[[newOrOldDoc? &timestamp=`[+createdon+]` &days=`3` &newchunk=`new_chunk` &oldchunk=`old_chunk`]]
      
      $days=isset($days)?$days:1;
      return time()-$timestamp<=86400*$days?'{{'.$newchunk.'}}':'{{'.$oldchunk.'}}';
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 7434
        • 24 Posts
        thank you for your reply

        i’m afraid i can’t make it work (or not sure how to use it)
          • 3749
          • 24,544 Posts
          Try this. Create a snippet called DaysOld and paste in the following code:

          <?php
          /* DaysOld snippet */
          if (empty($oldChunk) || empty($newChunk)) {
             return "Can't find the chunks";
          }
          $days=isset($days)?$days:3;
          $date2=$modx->documentObject['createdon'];
          $date1=time();
          $dateDiff    = $date1 - $date2; 
          $daysOld = floor($dateDiff/(60*60*24));
          return $modx->getChunk($daysOld <= $days? $newChunk : $oldChunk);
          ?>


          Create two chunks called OldChunk and NewChunk with what you want to display (the names are case-sensitive so be careful to get them right).

          Where you want the chunk content to appear, put this snippet tag:

          [!DaysOld? &oldChunk=`OldChunk` &newChunk=`NewChunk` !]


          It defaults to 3 days, but you can send a &days parameter to change that. It rounds down when calculating the days. If you want to round up, use ceil() instead of floor() in the snippet.
            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
            • 7434
            • 24 Posts
            Thank you, it works just fine. I just have one more question

            When I call the snippet from the document page (content) or the document template everything works fine

            What if i want to show the chunks (old or new) from a ditto call? To be more specific, I have "product list" pages which they list the children documents with a ditto call : [[Ditto? &ordertBy=`menuindex DESC` &tpl=`ProductFloats`]]

            If I include the snippet to the ProductFloats chunk then it does not evaluate and always shows the OldChunk

            ProductFloats.chunk

            <div class="Outer">
            <div class="PSKUgr">[+SKU_GR+]</div>
            <div class="PName">[+pagetitle+]&nbsp;<a href="/[~[+id+]~]">+</a></div>
            <div class="PPhoto><a rel="lightbox[Grouped]" title="[+pagetitle+]" href="assets/images/products/[+SKU_EN+].jpg">[!thumb? &filename=`[+SKU_EN+].jpg` &newxsize=`185` &newysize=`135`!]</a></div>

            <!-- New Product Conditional Begin -->
            [!DaysOld? &oldChunk=`OldChunk` &newChunk=`NewChunk` !]
            <!-- New Product Conditional END -->
            </div>


            Thanks again
            Dimitris
              • 3749
              • 24,544 Posts
              Quote from: dimbouk at May 28, 2010, 07:02 AM

              What if i want to show the chunks (old or new) from a ditto call? To be more specific, I have "product list" pages which they list the children documents with a ditto call : [[Ditto? &ordertBy=`menuindex DESC` &tpl=`ProductFloats`]]

              If I include the snippet to the ProductFloats chunk then it does not evaluate and always shows the OldChunk

              That’s because it’s getting the createdon field from the doc with the ditto tag.

              I haven’t tried it, but I think you can add &created=`[+createdon+]` to the snippet tag and, in the snippet, change

              $date2=$modx->documentObject['createdon'];


              to

              $date2=$created;
                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
                • 7434
                • 24 Posts
                Thank again

                it works fine now smiley
                  • 3749
                  • 24,544 Posts
                  I’m glad it worked for you. 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