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

    I am attempting to format a date using a snippet and getting an error. Here’s the code flow:

    1. MODx page uses this code to call "friendlydate" snippet with the page’s publication date

    [[friendlydate? &thisdate=`[*pub_date*]` ]]

    2. The "friendlydate" snippet is just this code:

    echo strftime("%V,%G,%Y",$thisdate);


    3. I am receiving this error, even tough $thisdate does in fact contain the pub_date unix time value:

    "strftime() expects parameter 2 to be long, string given"


    The same happens when I use date() instead of strftime(). If I pass a hard-coded value (instead of [*pub_date*]), I get the same error.

    Any ideas?

    Thanks!


      • 26903
      • 1,336 Posts
      I believe the parsing of the TV will return a string, like the parsing of a runSnippet call, I think the return value is coerced into a string type by the core, hence the error, although when seen it will look like the correct date value.
        Use MODx, or the cat gets it!
        • 8769
        • 3 Posts
        Thank you for your reponse shamblett.

        Yes, I think you are right. It appears that strftime is throwing the error b/c it is expecting an int value, and for some reason automatic type changing is not happening. When I attempt to type the variable using (int) in the snippet, I simply get a result of 0.

        I was incorrect in my last post about hard-coded values in the parameter - if I submit a string like this, it actually does work:
        [[friendlydate? &thisdate=`1199775600` ]]

        Surely someone has a workaround for this - people must often be wanting to show the publication date of a page, in a friendly format. However I can’t find anything on these forums or the larger Web. Do you have any suggestions of where to look?

        Thanks!


          • 8769
          • 3 Posts
          I created a workaround snippet for this problem, based on the AuthorTimestamp snippet: http://www.modxcms.com/AuthorTimestamp-0.5-0.html

          Here are the contents of my snippet, which just returns the pub_date of the current MODx page calling the snippet:

          $timestampformat = ’m/d/Y ’;
          $docInfo = $modx->getDocument($modx->documentIdentifier);
          echo date($timestampformat,$docInfo[’pub_date’]);

          The snippet does not require a parameter and thus there is no problem with MODx typing the template variable so that it can’t be used by date(). However, this workaround only works for the pub_date of the page, and I’d still like to know how we can get MODx to make friendly dates out of dates in TVs...