We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10997
    • 27 Posts
    I found I needed to write a snippet to format the ’created on’ date within a document so that I can show a non-timestamp value in the page.

    the code is as follows for now (testing, will use a placeholder if I can get it to work)
    <?php
    $id = $modx->documentIdentifier;
    $info = $modx->getDocument($id);
    $date = $info['createdon'];
    $format = '%B  %e  %Y';
    $date = (strftime($format, $date));
    echo $date;
    ?>
    


    Unfortunately, this returns nothing, just a blank value. This is my first attempt at snippet creation. I’ve checked the data all the way down the line, and it seems it is dying at the strftime... can anyone help me out?


      • 4041
      • 788 Posts
      Try this:

      <?php
      $currentdoc =$modx->documentObject['createdon'];
      $format = "%B  %e  %Y";
      if($currentdoc !=0){
        $date= strftime($format,$currentdoc);
      }else{
        $date ="No date available";
      }
      // set the placeholder here for usage later in your document or whatever
      $modx->setPlaceholder('Cdate',$date);  // [+Cdate+]
      
      // optionally output the results
      $output ="";
      $output .="[+Cdate+]";
      return $output;
      ?>
        xforum
        http://frsbuilders.net (under construction) forum for evolution
        • 36592
        • 970 Posts
        DateTime snippet can do this ...
        [!DateTime? &format=`%B %e %Y` &timestamp=`[*createdon*]`!]
          • 10997
          • 27 Posts
          smiley Thank you both - I swear I looked high and low for a ’DateTime’ snippet... must’ve been the late night. Appreciate the kind responses.
            • 22120
            • 21 Posts
            Tried this and the snippet call (copied and pasted exactly as provided above) displays only a "%" on the page - no time or date. Help/thoughts greatly appreciated - looks like DateTime snippet calls server time - is this a possible problem - am hosting with a provider - could this call be returning null and therefore getting zero "%" back? What server allowances/permissions/configs would this require?

            Apologies - really would like to get this working... any help appreciated greatly!

            Thanks! laugh
              • 36592
              • 970 Posts
              @oldwebdude

              This works for me.

              Are you possibly using RTE like TinyMCE to edit content ?
              If so, disable it and try again...
                • 22120
                • 21 Posts
                @tkfm

                BINGO! Yup - thanks tkfm - that was it. As soon as I set editor to none and re-entered the snippet call - problem solved.
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  If you really want/need the editor for a document with complex snippet calls, you can use a chunk, and put the snippet call in the chunk. Many snippets now come with an option to put configuration parameters in an external config file, which also avoids most problems with the editor trashing your snippet tags.
                    Studying MODX in the desert - http://sottwell.com
                    Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                    Join the Slack Community - http://modx.org
                    • 8790
                    • 526 Posts
                    The snippet DateTime could be improve for non english language.

                    Date formatting is always a pain for accentuated charset..

                    What about this ?:
                    <?php
                    /**
                     * DateTime 
                     * Makes a date and time... thingy. Now even thingier.
                     * Original author Alex Butter.
                     * 1.02 amended 08Aug06 by Paul Gregory, inspired by doze & breezer.
                     * 1.03 by Aramaki 27 Jan 2008
                     * [!DateTime? &format=`%B  %e  %Y` &timestamp=`[*createdon*]`!]
                     * 1.04 Charset improvement by Eric Lacomblez 29 Sep 2010
                     * [!DateTime? &format=`%B  %e  %Y` &timestamp=`[*createdon*]` &local_time=`fr_FR`!]
                     */
                    
                    /* force local_time for correct php date format */  
                    if(!isset($local_time)) {
                        $local_time = "fr_FR";
                    }
                     setlocale (LC_TIME, $local_time);
                    
                    if(!isset($format)) {
                        $format="%d-%m-%Y %H:%M:%S";
                    }
                    if(!isset($timestamp)) {
                        $timestamp=time();
                    }
                    
                    if(!isset($shift)) {
                        $shift=0;
                    }
                    
                    // Do proper charset
                    
                    if ($modx->config['modx_charset']=='UTF-8') {
                    	return utf8_encode(strftime($format, $timestamp+$shift+$modx->config['server_offset_time']));
                    } else {
                    	return strftime($format, $timestamp+$shift+$modx->config['server_offset_time']);
                    }
                    
                    ?>
                    


                    Thank you
                      Schtroumpf Grognon - Grouchy Smurf
                      ---------------------------------
                      Faites pas attention.. - Don&#39;t pay attention
                      http://www.dzi-neo.net