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

    Got a fairly straight forward question (I hope). I’m using a template variable, in this case called [*myDate*] to create a custom date entry for blog posts - url found here http://ditto.modxcms.com/tutorials/sort-by-a-date-tv.html. I’ve successfully gotten the date working in relation to the use of Ditto (with &dateSource and &dateFormat, etc). However, I was also hoping to put [*myDate*] in the template for the Blog entries so that the custom date would display automatically in the blog entry but because it’s set up as unixtime, it simply shows the unix timestamp. I have absolutely no idea on how to control or convert this in the template like I did with the Ditto call. Is there a way to do this or am I using this incorrectly? Maybe there is an alternative way to easily get the result I was hoping for? I’ve poured through the forum and Google but can’t seem to find what I need. huh

    I appreciate any help anyone may be able to offer.
      • 20413
      • 2,877 Posts
      If I understand, you got the datetv working in the ditto listings and in the snippet tpl!
      Now you need a to use the TV for the template of the document/resource itself!

      Simple way: use pub_date or createdon instead of your own TV.
      OR (not tested):
      [[Ditto? &documents=`[*id*]` &save=`3` &dateSource=`yourTV`]]
      [+date+]


      The benefit of using pub_date or createdon is that you don’t have to set the TV every time
      ...and just use [*pub_date*] or [*createdon*] in the template.
        @hawproductions | http://mrhaw.com/

        Infograph: MODX Advanced Install in 7 steps:
        http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

        Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
        http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
        • 16278
        • 928 Posts
        Ditto uses its built-in copy of PHx (extended placeholder) formatting to convert timestamps to human-readable dates in the tpl template chunks for the listing pages. But the full post display isn’t using Ditto, so you need a snippet to do the conversion. Something like
        <?php
        // DateFormat
        // apply formatting to Unixtime date
        // parameters: 
        // &dateSource - Unix timestamp
        // &dateFormat: strftime format string
        
        $result = NULL;  // or 'bad date' or whatever
        
        if (!empty($dateSource) && $dateSource > 0) {
            $result = strftime($dateFormat, $dateSource);
        }
        
        return $result;
        ?>
        and a snippet call in your site template for posts
        [!DateFormat? &dateSource=`[*myDate*]` &dateFormat=`%d %b %Y` !]

        cool KP
          • 36592
          • 970 Posts
            • 275
            • 12 Posts
            Wow! You guys are quick.

            @mrhaw

            Considered using pub_date or createdon but wanted to simulate the ability to control display date for blog entries like you can with Wordpress - sometimes it is very beneficial. I didn’t realize Ditto could be self referential (like using something such as &documents=`[*id*], I thought it always had to be in the container page and aggregate subpages). So that way works because I can use a Ditto call in each blog entry and control the date format as before via Ditto.

            @kp52

            That way works too! In fact, this was originally what I was thinking when asking for help. Now I’m not sure which method would be better to use - your solution or mrhaw’s. Would one be more efficient or take less load time then the other? One being Ditto in the HTML template that refers to a chunk for formatting. The other being putting those necessary variables directly in the HTML template (pagetitle, author, date, etc) and using the DateFormat snippet to convert the date to something human readable.

            I just want to make sure I’m not using a method that is extremely resource heavy unnecessarily.

            Thanks guys!
              • 20413
              • 2,877 Posts
              There is one more options:
              To use the Document Variable (pub_date or createdon) or yourTV and use PHx plugin to format it:
              <strong>[*pub_date:date=`%d-%b-%y %H:%M`*]</strong>


              I haven’t really experienced any difference in load or speed from any of these methods.
              Keith’s (kp52) custom snippet is about the same code as the PHx Plugin would run.

              //The snippet #1 - http://modxcms.com/forums/index.php/topic,18872.msg161906.html#msg161906
                @hawproductions | http://mrhaw.com/

                Infograph: MODX Advanced Install in 7 steps:
                http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

                Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
                http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower