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

    I am trying to create a snippet which calls a date from a template variable, then reformats that date into a different format that is more user friendly.
    The snippet pulls the correct data from the TV, because when I print $calstartdate the correct dates show. However, when I use the strtotime, it says that all the dates are 1969-12-31 17:00:00. I’m not sure why this is. Here is the snippet below.

    <?php
    global $modx;
    
    $rawvalue = $modx->getTemplateVarOutput('CalStartDate');
    $calstartdate = "[+CalStartDate+]";
    
    echo date( "Y-m-d H:i:s", strtotime( "$calstartdate" ) );
    ?>


    I’m not quite sure what is going on here - I think it might be an issue with parsing the dates.
      • 10487 MODX Staff
      • 1,535 Posts
      The reason you get the odd date returned from the snippet is that all you are passing to the strtotime() call is the string ’[+CalStartDate+]’ (i.e. not the actual date value, MODx doesn’t render these tags in snippet code but would do when it’s echoed/outputted from the snippet)

      You may be better using something like:
      return date( "Y-m-d H:i:s", strtotime( $modx->documentObject['CalStartDate'] ) );

        Garry Nutting
        Senior Developer
        MODX, LLC

        Email: [email protected]
        Twitter: @garryn
        Web: modx.com
        • 21652
        • 2 Posts
        Thanks for the response.
        Wouldn’t I have to turn the array being fetched into a string? I now receive an error stating:
        Error: strtotime() expects parameter 1 to be string, array given.
          • 10487 MODX Staff
          • 1,535 Posts
          Ah, sorry nmcyber - you’ll need to use: $modx->documentObject[’CalStartDate’][1]
            Garry Nutting
            Senior Developer
            MODX, LLC

            Email: [email protected]
            Twitter: @garryn
            Web: modx.com