[pmwiki-devel] {(ftime ... )} extension.

Stirling Westrup sti at pooq.com
Thu Oct 18 17:30:15 CDT 2007


I recently ran into a problem where I needed to display the same date several
times on one page, but in different locales. The solution that I came up with
was to extend the {(ftime...)} function to take an optional third parameter
'lc' which takes a locale in which to render the date. Since I use ftime in a
number of places on my site, I was very careful to preserve all existing
functionality, so the following should be a safe drop-in replacement. If you
have a use for it, you can just copy what follows into config.php. I would
like to see this extension added to the core, if other folks find it useful too.

$MarkupExpr['ftime'] = 'lc_ftime($args, $argp)';

##   lc_ftime handles {(ftime fmt=x when=y lc=z)} expressions.
##
function lc_ftime($args = NULL, $argp = NULL)
  { global $TimeFmt, $Now, $FTimeFmt;

    ## get the format string
    if( isset($argp['fmt']) )
      $fmt = $argp['fmt'];
    else if (strpos(@$args[0], '%') !== false)
      $fmt = array_shift($args);
    else if (strpos(@$args[1], '%') !== false)
      list($fmt) = array_splice($args,1,1);
    else
      {
        SDV($FTimeFmt, $TimeFmt);
        $fmt = $FTimeFmt;
      }

    ## determine the timestamp
    if( isset($argp['when']) )
      list($time, $x) = DRange($argp['when']);
    else if (@$args[0] > '')
      list($time, $x) = DRange(array_shift($args));
    else $time = $Now;

    ## get the locale
    if( isset($argp['lc']) )
      $locale=$argp['lc'];
    else
      $locale=array_shift($args);

    ##  make sure we have %F for ISO dates, %s for unix timestamps.
    $fmt = str_replace(array('%F', '%s'), array('%Y-%m-%d', $time), $fmt);

    ##  handle locale switch, if need be.
    if( isset($locale) )
      {
        $oldlc = setlocale(LC_ALL,'0');
        setlocale(LC_ALL,$locale);
        $ret = strftime($fmt,$time);
        setlocale(LC_ALL,$oldlc);
      }
    else
      $ret = strftime($fmt,$time);
    return $ret;
  }






More information about the pmwiki-devel mailing list