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

    I’m new to MODx, using 2.0.1 and character encoding UTF-8. I’m trying to format a date in a Template. It is the pub_date of the current resource. I have this line of code:

    <p class="dt"><?php strftime("%A, %d %M %Y", $modx->documentObject[$pub_date]) ?></p>

    It returns (with the correct CSS styling!):

    documentObject[$pub_date]) ?>

    I have tried a lot of variations on the code with no success. Have also tried using the snippet FormatDate - it returns a completely wrong date. Can someone please spot where I’m going wrong?

    Thanks.
      • 33202
      • 9 Posts
      OK, I’ve got FormatDate to work by calling it uncached. Thanks to an earlier thread which I’ve just found for suggesting that.

      However, would still really appreciate any thoughts on why the code I quoted above does not work. Am I trying to do the right thing but in the wrong place? Or am I making another newbie mistake through not really understanding what I’m doing? <g>
        • 3749
        • 24,544 Posts
        Quote from: yorkie at Nov 14, 2009, 07:01 PM

        OK, I’ve got FormatDate to work by calling it uncached. Thanks to an earlier thread which I’ve just found for suggesting that.

        However, would still really appreciate any thoughts on why the code I quoted above does not work. Am I trying to do the right thing but in the wrong place? Or am I making another newbie mistake through not really understanding what I’m doing? <g>

        It doesn’t work because you can’t have PHP code embedded in HTML in MODx. You’d have to move that code to a snippet and put a snippet tag in the template (or wherever you want the date to appear):

        Tag: [!MyDateSnippet!]

        MyDateSnippet contents:

        <?php
        return strftime("%A, %d %M %Y", $modx->documentObject[$pub_date]);
        ?> 

          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
          • 33202
          • 9 Posts
          BobRay,

          It doesn’t work because you can’t have PHP code embedded in HTML in MODx.

          Thanks! Ah - I’m gradually getting to grips with what works where in MODx. I’ll add that to my list.

          Being new to PHP as well, there is a lot to take in. But knowing that the syntax is correct is a great help. I’ll create my own snippet as you suggest - my first.
            • 33202
            • 9 Posts
            For the sake of any other newbie reading this thread, I made 2 changes to the code for my snippet to work as intended:

            • replaced %M with %B to give full month name rather than minutes (those time formats are a little confusing!)
            • replaced $pub_date with ’pub_date’, including the quotes. Using $pub_date returns Thursday, 01 January 1970 whatever the actual published date.

            So, the final version of my snippet, called from a (resource) Template is:

            <?php
            return strftime("%A, %d %B %Y", $modx->documentObject['pub_date']);
            ?>