We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18399
    • 60 Posts
    I have a template where I want to format a date within a template variable.

    This code:

    <p class="articleDate">
    [*articleDate:date=`%A %e %B %Y`*] - [*articleDate*]</p>


    produces the following output:

    Wednesday 31 December 1969 - 14-05-2009 12:28:07

    So in case of using PHx its 1969, in the case of printing the variable we see it is correct. This is nuts. Its even given as an example in the PHx documentation!

    It has to be said that: http://modxcms.com/extras.html?view=package/view&package=409 (SiteLastUpdated) is a much classier piece of code where you can use English names for the dates for the time of the last update of the whole website:

    eg Site last updated: Sunday 24th May 2009 05:29:08

    Here is the code:

    $dateFormat = isset($dateFormat)? $dateFormat:’j F Y’;

    return date($dateFormat, $modx->db->getValue($modx->db->select(’editedon’, $modx->getFullTableName(’site_content’), ’published=1’, ’editedon DESC’, 1)));

    How would you change it to read a specific template variable?

    Mark
      • 22427
      • 793 Posts
      Because your TV [*articleDate*] is not fetched from the DB (I assume) it should be enough to write
      return date($dateFormat, $modx->getTemplateVarOutput(true, $id)['articleDate'])
      (or similar, I’m not very familiar with the MODx API).
        • 18399
        • 60 Posts
        Quote from: ottogal at May 24, 2009, 01:43 PM

        Because your TV [*articleDate*] is not fetched from the DB (I assume) it should be enough to write
        return date($dateFormat, $modx->getTemplateVarOutput(true, $id)['articleDate'])
        (or similar, I’m not very familiar with the MODx API).

        I tried a few of these and similar, throws an error. But this is the kind of thing I had in mind. I really like Modx and am committed to trying to get this to work for my site but the documentation and the new book (Modx Web Development) still leave horrible gaps. Actually if someone posts the right code to make the change I think it would help me with many more things.

        Mark
          • 18399
          • 60 Posts
          I have a new code:
          $dateFormat = isset($dateFormat)? $dateFormat:’j F Y’;
          $tv = "articleDate";
          $tvarray = $modx->getTemplateVarOutput($tv);
          $status = $tvarray["articleDate"];
          print $status;
          return date($dateFormat, $status);

          I don’t understand why $status has the right value but the output is still wrong. Anybody understand what might be wrong?

          14-05-2009 12:28:07
          Wednesday 31st December 1969

          Mark

            • 36592
            • 970 Posts
            return date($dateFormat, $status);

            I’m not a PHP expert, so this might be wrong idea ...

            I think the second parameter of date() function should be UNIX timestamp.
            ( $status holds character based date like "14-05-2009 12:28:07". )
              • 18399
              • 60 Posts
              Quote from: tkfm at May 25, 2009, 05:10 AM

              return date($dateFormat, $status);

              I’m not a PHP expert, so this might be wrong idea ...

              I think the second parameter of date() function should be UNIX timestamp.
              ( $status holds character based date like "14-05-2009 12:28:07". )

              Nope, you totally have the right idea!! Using information at: http://bytes.com/groups/php/768105-convert-unix-timestamp I used

              $dateFormat = isset($dateFormat)? $dateFormat:’j F Y’;
              $tv = "articleDate";
              $tvarray = $modx->getTemplateVarOutput($tv);
              $date = $tvarray["articleDate"];
              $status = strtotime($date);
              return date($dateFormat, $status);

              to completely solve my problem. I’m also presuming that I had the wrong format for the date for PHx, although I don’t care about that anymore.

              Now in the template: [!SiteLastUpdated? &dateFormat=`l jS F Y H:i:s`!] returns Thursday 14th May 2009 12:28:07 from which I might remove the time but its entirely what I was looking for.

              To summarise, I can now create a template variable with the date and time of the article (I have some very old articles I may want to put the correct date on) and I can now completely control the output using these parameters (http://nl3.php.net/date). I did this by changing the Modx Snippet SiteLastUpdated to the above code, which may include at least one line too many, I’m not sure.

              According to the above article strtotime($date) (behaves unreliably with dd/mm/(yy)yy dates) which I am not sure is going to be a problem. But I’m very happy with the output. I can’t help feeling that Modx should have this out of the box, but there you go, I learned quite a bit just doing this, hope it might be useful to someone.

              Many thanks for the pointers!

              Mark