Prado\I18N\core\DateFormat::format PHP Method

format() public method

Format a date according to the pattern.
public format ( $time, $pattern = 'F', $charset = 'UTF-8' ) : string
return string formatted date time.
    public function format($time, $pattern = 'F', $charset = 'UTF-8')
    {
        if (is_numeric($time)) {
            //assumes unix epoch
            $time = floatval($time);
        } else {
            if (is_string($time)) {
                $time = @strtotime($time);
            }
        }
        if ($pattern === null) {
            $pattern = 'F';
        }
        $s = Prado::createComponent('System.Util.TDateTimeStamp');
        $date = $s->getDate($time);
        $pattern = $this->getPattern($pattern);
        $tokens = $this->getTokens($pattern);
        for ($i = 0, $k = count($tokens); $i < $k; ++$i) {
            $pattern = $tokens[$i];
            if ($pattern[0] == "'" && $pattern[strlen($pattern) - 1] == "'") {
                $sub = preg_replace('/(^\')|(\'$)/', '', $pattern);
                $tokens[$i] = str_replace('``````', '\'', $sub);
            } else {
                if ($pattern == '``````') {
                    $tokens[$i] = '\'';
                } else {
                    $function = $this->getFunctionName($pattern);
                    if ($function != null) {
                        $fName = 'get' . $function;
                        if (in_array($fName, $this->methods)) {
                            $rs = $this->{$fName}($date, $pattern);
                            $tokens[$i] = $rs;
                        } else {
                            throw new Exception('function ' . $function . ' not found.');
                        }
                    }
                }
            }
        }
        return I18N_toEncoding(implode('', $tokens), $charset);
    }

Usage Example

Beispiel #1
0
 public function testCustomPatterns()
 {
     $dateFormatter = new DateFormat();
     $time = @mktime(9, 9, 9, 9, 1, 2004);
     $pattern = "'Hello' EEEE, 'it should be' MMM yyyy HH:mm:ss!!!";
     $wants = 'Hello Wednesday, it should be Sep 2004 09:09:09!!!';
     $this->assertEquals($wants, $dateFormatter->format($time, $pattern));
 }
All Usage Examples Of Prado\I18N\core\DateFormat::format