<?
// Simple plugin: an improved date function -- accepts PHP date operators
// VERSION 1.0
// NO WARRANTY - USE AT YOUR OWN RISK!
class NP_BetterDate extends NucleusPlugin {
 
    function getName() { return 'Better Date'; }
    function getAuthor()  { return 'Nate Silva'; }
    function getURL() { return 'http://filteredlife.org/betterdate/'; }
    function getVersion() { return '1.0'; }
    function supportsFeature($what) {
        switch($what) {
    case 'SqlTablePrefix':
        return 1;
    default:
        return 0;
        }
    }
 
    function getDescription() {
        $s = 'An improved date function which accepts all PHP date operators. ';
        $s .= 'Call with <span style="white-space:nowrap;">&lt;%BetterDate(<b>posted</b>, 
<i>format</i>)%&gt;</span>, ';
        $s .= '<span style="white-space:nowrap;">&lt;%BetterDate(<b>postedutc</b>, 
<i>format</i>)%&gt;</span>, ';
        $s .= '<span style="white-space:nowrap;">&lt;%BetterDate(<b>now</b>, <i>format</
i>)%&gt;</span> ';
        $s .= 'or <span style="white-space:nowrap;">&lt;%BetterDate(<b>nowutc</b>, 
<i>format</i>)%&gt;</span>. ';
        $s .= 'The <i>format</i> is a PHP date() string, or the special string 
<i>iso8601</i>, ';
        $s .= 'which will use the international standard format. ';
        $s .= 'A special format <i>tzwithcolon</i> is also available as a hack to make 
Atom 0.2 ';
        $s .= '\'issued\' dates validate at feeds.archive.org. The <i>tzwithcolon</i> 
format only ';
        $s .= 'applies to the <b>posted</b> date.';
        return $s;
    }
 
    function doTemplateVar(&$item,$datetype,$format) {
        $datetype = strtolower($datetype);
        $format_iso = 'Y-m-d\TH:i:sO';
        $format_iso_utc = 'Y-m-d\TH:i:s\Z';
 
        if ($datetype == 'now') {
            if (strtolower($format) == 'iso8601')
                echo date($format_iso);
            else
                echo date($format);
        }
        elseif ($datetype == 'nowutc') {
            if (strtolower($format) == 'iso8601')
                echo gmdate($format_iso_utc);
            else
                echo gmdate($format);
        }
        elseif ($datetype == 'posted') {
            if (strtolower($format) == 'iso8601')
                echo date($format_iso, $item->timestamp);
            elseif (strtolower($format) == 'tzwithcolon') {
                // for some reason the feed validator at archive.org expects
                // the atom/echo/pie "issued" date timezone to have a colon
                $tz = date('O', $item->timestamp);
                $tz = substr($tz, 0, 3) . ':' . substr($tz, 3, 2);
                echo date('Y-m-d\TH:i:s', $item->timestamp) . $tz;
            }
            else
                echo date($format, $item->timestamp);
 
        }
        elseif ($datetype == 'postedutc') {
            if (strtolower($format) == 'iso8601')
                echo gmdate($format_iso_utc, $item->timestamp);
            else
                echo gmdate($format, $item->timestamp);
        }
    }
}
?>
np_betterdate_code.txt · Last modified: 2006/07/05 13:03 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki