[pmwiki-users] calendar date title converter

Patrick R. Michaud pmichaud at pobox.com
Thu Sep 8 16:14:55 CDT 2005


On Thu, Sep 08, 2005 at 08:43:40PM +0100, Hans wrote:
> I am wondering if it is possible to create a custom markup variable
> from the number string of a calendar page.

Sure!  How about something like:

    function DName($pagename, $fmt) {
      if (!preg_match('/\\.(\d\d\d\d)(\d\d)(\d\d)$/', $pagename, $match))
        return $pagename;
      if (!$fmt) $fmt = '%b %e %Y';
      $gmt = mktime(0, 0, 0, $match[2], $match[3], $match[1]);
      return strftime($fmt, $gmt);
    }
    
    Markup('{$DName}', '{$var}',
      '/{\\$DName(:(.+?))?}/e',
      "DName(\$pagename, '$2')");

With a page name of 20050908, the markup "{$DName}" 
returns "Sep  8 2005".  One can also specify the date
format desired as part of the markup (a la strftime()), thus

    {$DName:%a %b %d}         "Thu Sep 08"
    {$DName:%d-%b-%Y}         "08-Sep-2005"
    {$DName:%A, %B %d, %Y}    "Thursday, September 08, 2005"

    {$DName:%e}               "8"
    {$DName:%B}               "September"
    {$DName:%Y}               "2005"


Of course, the day and month names are localized according to
whatever language happens to be in effect.  :-)  

See http://www.pmwiki.org/wiki/Test/20050908 for an example and
sandbox (it's currently enabled for the entire Test group 
on pmwiki.org).

If {$DName} is used on a page that isn't named with
something in YYYYMMDD format, it just returns the pagename 
without any formatting.

Enjoy!

Pm




More information about the pmwiki-users mailing list