We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36541
    • 222 Posts
    I'm setting up RSS feeds for a multilingual site. The RSS specs are crazy enough to require the pubDate element conform to RFC822 date and time format which uses names for day of the week and month. In multilingual site that is a problem, as MODX's cultureKey respects the site/context language which results in invalid pubDate format.

    RFC822 requires this:
    <pubDate>Thu, 14 Feb 2013 13:15:00 +0100</pubDate>
    But for Polish locale I get this:
    <pubDate>czw, 14 lut 2013 13:15:00 +0100</pubDate>


    The question is: how to set locale for :date output filter?

    I'd expect something like this should be possible:
    <pubDate>[[+publishedon:strtotime:locale=`en_US`:date=`%a, %d %b %Y %H:%M:%S %z`]]</pubDate>


    I followed instructions in getResources.Building a RSS feed from MODX docs.

    This question has been answered by gadamiak. See the first response.

      This is the web: the only thing you know about who will come is that you don't know who will come.
      • 3749
      • 24,544 Posts
      From the PHP strtotime man page:

      you can't really set a locale for strtotime. If you're American, you see 11/12/10 and think "12 November, 2010". If you're Australian (or European), you think it's 11 December, 2010. If you're a sysadmin who reads in ISO, it looks like 10th December 2011.

      The best way to compensate for this is by modifying your joining characters. Forward slash (/) signifies American M/D/Y formatting, a dash (-) signifies European D-M-Y and a period (.) signifies ISO Y.M.D.

      Observe:

      <?php
      echo date("jS F, Y", strtotime("11.12.10"));
      // outputs 10th December, 2011

      echo date("jS F, Y", strtotime("11/12/10"));
      // outputs 12th November, 2010

      echo date("jS F, Y", strtotime("11-12-10"));
      // outputs 11th December, 2010
      ?>

      All the output modifier does is call strtotime, so in theory, it should work. If not, you'd have to write your own custom output modifier.
        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
        • 40706
        • 128 Posts
        As far as i see, strtotime works. But in an RSS Feed, the timestamp has always be in the american locale as you already wrote.
        So it would be easiest to just make an small snippet, setting the right locale just for this document. Instead of setting it hundreds of times in the getResuoces rows.

        <?php
        setlocale(LC_TIME, 'en_US');


        And call it before getResouces.
          • 36541
          • 222 Posts
          @BobRay: The question is not about the timestamp content but it's format. The problem is in names (not numbers!) used in the time format.

          @Michael: I tried that but to no avail. Looks like the locale setting gets reset by parser in the meantime.

          Here's my quick snippet:

          <?php
          /*
           * (Re)Sets locale
           *
           * Usage
           * - [[locale? &category=`LC_TIME` &locale=`en_US`]]
           * - [[...:locale=`category==LC_TIME||locale==en_US`:...]]
           *
           * When called without &locale will reset it to system setting
           *
           * Params:
           * - category: a named constant specifying the category of the functions 
           * affected by the locale setting
           * - locale: a locale to set, eg. "en_US"
           */
          
          /* Initialize */
          $category = $modx->getOption('category', $scriptProperties, 'LC_ALL');
          $config = $modx->getConfig();
          $locale = ($locale = $modx->getOption('locale', $scriptProperties, '')) ? $locale : $config['locale'];
          unset($config);
          
          /* If used as output filter */
          if (isset($input)) {
            $modx->getService('parser', 'modParser');
            $options = $modx->parser->parseProperties($options);
            $locale = (array_key_exists('locale', $options)) ? $options['locale'] : $locale;
            $category = (array_key_exists('category', $options)) ? $options['category'] : $category;
            unset($options);
          }
          
          return setlocale($category, $locale);
          


          I used it before and after getResources call. But it didn't work and locale doesn't change. You may check current setting with [[locale? &category=`LC_TIME` &locale=`0`]]. I used it in getResources tpl chunk, but it didn't work either. The same with output filter.

          Looks like I have to write my custom output filter, which accepts both format for strftime and a locale.= [ed. note: gadamiak last edited this post 11 years, 2 months ago.]
            This is the web: the only thing you know about who will come is that you don't know who will come.
            • 3749
            • 24,544 Posts
            Quote from: gadamiak at Feb 17, 2013, 03:35 PM
            @BobRay: The question is not about the timestamp content but it's format. The problem is in names (not numbers!) used in the time format.

            Timestamps only have one format (seconds since Jan 01 1970), so the problem is not about the timestamp format, it's about the format you get when you convert a timestamp to a more readable format. That said, I think you're right that the method I suggested won't work with MODX output modifiers.

            If the author is right, date() apparently infers the locale from the format of the date string, so in a custom snippet you could convert the timestamp it to a string with strftime, parse it, then go back the other way with strtotime() and date(), but I'm sure there's an easier way to do what you want.

            You're not on a Windows server, by any chance? That changes things.
              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
            • discuss.answer
              • 36541
              • 222 Posts
              I managed to solve this using the following snippet of mine:

              <?php
              /*
               * (Re)Sets locale
               *
               * Set locale for time to `en_US.utf8`:
               *
               *   [[locale? &category=`LC_TIME` &locale=`en_US.utf8`]]
               *   [[...:locale=`category==LC_TIME||locale==en_US.utf8`:...]]
               *
               * Reset to system locale for all categories:
               *
               *   [[locale]]
               *
               * Params:
               * - category: a named constant specifying the category of the functions 
               * affected by the locale setting
               * - locale: a locale to set, eg. "en_US.utf8"
               */
              
              /* Initialize */
              (string) $category = $modx->getOption('category', $scriptProperties, 'LC_ALL');
              $config = $modx->getConfig();
              (string) $locale = ($locale = $modx->getOption('locale', $scriptProperties, '')) ?
                $locale : $syslocale = $config['locale'];
              unset($config);
              
              /* If used as output filter */
              if (isset($input)) {
                $modx->getService('parser', 'modParser');
                $options = $modx->parser->parseProperties($options);
                $locale = (array_key_exists('locale', $options)) ?
                  (string) $options['locale'] : $locale;
                $category = (array_key_exists('category', $options)) ?
                  (string) strtoupper($options['category']) : $category;
                unset($options);
              }
              
              setlocale($category, $locale);
              
              if (isset($input))
                return $input;
              
              return;
              


              I used it before getResources call setting LC_TIME to en_US.utf8 and resetting it to system setting after that call. One thing worth noting is the proper transcription of the locale, which must match what is installed on the host OS.

              The snippet above works fine except output filter mode, which I couldn't manage to work.
                This is the web: the only thing you know about who will come is that you don't know who will come.
                • 40016
                • 32 Posts
                Hi all,

                I created an output modifier to solve this which seems way easier than

                Snippet Code "dateLocale":
                <?php
                setlocale(LC_ALL, 'en_US');
                
                return strftime($options,$input);


                Usage:
                [[+publishedon:strtotime:dateLocale=`%a, %d %b %Y %H:%M:%S %z`]]


                I use it in a RSS-feed to get the correct format for the date.