We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21122
    • 153 Posts
    I’m using Ditto to pull in some news stories. When I go to one of these stories, I can display the title using [*pagetitle*] and I can display the content using [*content*], but how do I display the date? It doesn’t seem to be listed http://wiki.modxcms.com/index.php/Placeholders_used_by_MODx_Pages_and_Templates?

    Any idea?

    Thanks
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      The MODx variable for that is [*createdon*], but since that’s a UNIX timestamp you don’t want to use it. Ditto has a placeholder [+date+] that converts the create date to a human-readable form, and you can specify the exact format you want to use with the &dateFormat parameter in the snippet call, using any PHP valid strftime option.

      You can also tell Ditto to use the publish date or the last edited date instead of the createdon date.

      http://ditto.modxcms.com/files/snippet-ditto-php.html#dateSource
        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
        • 21122
        • 153 Posts
        Thank you for replying Susan.

        I’m not quite sure if your response answers my question though. I’ve tried using [+date+] without success, I think because I’m not using it within a Ditto call. Although I’m using Ditto on the home page to show the news items (and I’ve used [+date+] and dateFormat here), I’ve just set up a link to each news story (which is essentially just a modx page). [*createdon*] shows a series of random number (the UNIX timestamp I presume) but I’m not sure how this can be converted in my case.

        Could you show me an example please?

        Thanks
          • 4310
          • 2,310 Posts
          Here’s a Unixtime TV to Human readable date snippet :
          <?php
          $output = '';
          if(isset($tv_name) && isset($date_format)){
          $tv = $modx->getTemplateVarOutput(true, $modx->documentIdentifier);
          $field = $tv[''.$tv_name.''];
          if(!empty($field)){
          $output = strftime(''.$date_format.'', $field);
          }
          }
          return $output;
          ?>

          Use like this :
          [!DateFormatter? &tv_name=`createdon` &date_format=`%a %d %b %H:%M:%S %Y`!]

          Where the tv_name is a unixtime TV and the date_format is any PHP valid strftime
            • 21122
            • 153 Posts
            Thanks for that.

            I’m still not sure how to use the code above though. I’ve created a snippet with the PHP code in, and named it ’date-converter’.

            Where do I put the DateFormatter call? And do I need to call &tv_name anything in particular?

            Please forgive my ignorance, I’m pretty new to modx!

            Thanks
              • 4310
              • 2,310 Posts
              Where do I put the DateFormatter call? And do I need to call &tv_name anything in particular?
              You would add :
              [!date-converer? &tv_name=`createdon` &date_format=`%a %d %b %H:%M:%S %Y`!]

              Wherever you want that date to appear, either on the page or possibly in the template if the news stories use a unique template.

                • 21122
                • 153 Posts
                Thanks very much Bunky, it works like a charm!