Symfony Helper -> format_date, how to use

It’s a Symfony thing in sfDateFormat::getPattern().

You can use this helper like this following example:

<?php echo format_date($blog_post->getCreatedAt(), "D“) ?>

The D is a pre-formatting from symfony, above a list about others pre formats options.

switch ($pattern)
    {
      case 'd':
        return $this->formatInfo->ShortDatePattern;
        break;
      case 'D':
        return $this->formatInfo->LongDatePattern;
        break;
      case 'p':
        return $this->formatInfo->MediumDatePattern;
        break;
      case 'P':
        return $this->formatInfo->FullDatePattern;
        break;
      case 't':
        return $this->formatInfo->ShortTimePattern;
        break;
      case 'T':
        return $this->formatInfo->LongTimePattern;
        break;
      case 'q':
        return $this->formatInfo->MediumTimePattern;
        break;
      case 'Q':
        return $this->formatInfo->FullTimePattern;
        break;
      case 'f':
        return $this->formatInfo->formatDateTime($this->formatInfo->LongDatePattern, $this->formatInfo->ShortTimePattern);
        break;
      case 'F':
        return $this->formatInfo->formatDateTime($this->formatInfo->LongDatePattern, $this->formatInfo->LongTimePattern);
        break;
      case 'g':
        return $this->formatInfo->formatDateTime($this->formatInfo->ShortDatePattern, $this->formatInfo->ShortTimePattern);
        break;
      case 'G':
        return $this->formatInfo->formatDateTime($this->formatInfo->ShortDatePattern, $this->formatInfo->LongTimePattern);
        break;
      case 'i':
        return 'yyyy-MM-dd';
        break;
      case 'I':
        return 'yyyy-MM-dd HH:mm:ss';
        break;
      case 'M':
      case 'm':
        return 'MMMM dd';
        break;
      case 'R':
      case 'r':
        return 'EEE, dd MMM yyyy HH:mm:ss';
        break;
      case 's':
        return 'yyyy-MM-ddTHH:mm:ss';
        break;
      case 'u':
        return 'yyyy-MM-dd HH:mm:ss z';
        break;
      case 'U':
        return 'EEEE dd MMMM yyyy HH:mm:ss';
        break;
      case 'Y':
      case 'y':
        return 'yyyy MMMM';
        break;
      default :
        return $pattern;
    }

That’s really good to don’t lose time.

3 Responses to this post.

  1. rubem's Gravatar

    Posted by rubem on 28.05.09 at 19:00

    Hi Felipe, great post. Let me extend it by sharing my experience with format_date().

    I’m working on a symfony project where all dates need to follow a custom format.

    I was able to achieve the date format I needed by specifying it manually…

    setCulture(’fr_CA’) ?> // fset culture to French Canadian

    // Culture-Dependent Helper

    // juil. 03, 2010

    or

    // juillet 03, 2010

    or

    // juil. 13, 10

    or

    ….

    Cheers,
    Rubem

  2. rubem's Gravatar

    Posted by rubem on 28.05.09 at 19:00

    php code was stripped out…

    anyway, try this as well…

    format_date(’2010-07-13′,’MMM dd, yy’)

    cheers,

    rubem

  3. Gilmar Pupo's Gravatar

    Posted by Gilmar Pupo on 28.05.09 at 19:00

    Other case:

    public function getDate()
    {
    use_helper(’Date’);
    return format_date($this->getCreatedAt(),’dd.MM.yyyy’);
    }
    //return 03.05.2010

    []’s!
    @gpupo

Respond to this post