You are here

protected function DatexObject::formatPHP in Datex 7.2

Use php to format date

1 call to DatexObject::formatPHP()
DatexObject::format in datex_api/datex_api.class.inc
Similar to DateTime::format().

File

datex_api/datex_api.class.inc, line 646
Provides an API to work with dates.

Class

DatexObject
Base class for localized DateTime.

Code

protected function formatPHP($format) {
  static $t = FALSE;
  if (!$t) {
    $t = _datex_get_translator();
  }
  $names = $this
    ->getInfo('names');
  $monthDays = $this
    ->getInfo('month_days');
  $this
    ->setConvert();
  $this
    ->setIsLeap();

  // Should contain N, w, W, z, S, M,
  $this
    ->setCalendarFormats();
  $ampm = $this
    ->xformat('a');

  // A series of calls to str replace can not be used since format may
  // contain \ charachter which should not be replaced.
  $formatted = '';
  for ($i = 0; $i < strlen($format); $i++) {
    $f = $format[$i];

    // If extended class wants to override some formats, It should put it
    // in here.
    if (isset($this->calendarFormats[$f])) {
      $formatted .= $this->calendarFormats[$f];
    }
    else {
      switch ($f) {
        case '\\':
          $date .= $format[++$i];
        case 'd':
          $formatted .= sprintf('%02d', $this->day);
          break;
        case 'q':
          $formatted .= $t($names['day_abbr'][$this->dayOfWeek]);
          break;
        case 'D':
          $formatted .= $t($names['day_abbr_short'][$this->dayOfWeek]);
          break;
        case 'j':
          $formatted .= intval($this->day);
          break;
        case 'l':
          $formatted .= $t($names['day'][$this->dayOfWeek]);
          break;
        case 'S':
          $formatted .= $t($names['order'][$this->day - 1]);
          break;
        case 'W':
          $value_w = strval(ceil($this->dayOfYear / 7));
          $formatted = $formatted . $value_w;
          break;
        case 'z':
          $formatted = $this->dayOfYear;
          break;
        case 'M':
        case 'F':
          $formatted .= $t($names['months'][$this->month - 1]);
          break;
        case 'm':
          $formatted .= sprintf('%02d', $this->month);
          break;
        case 'n':
          $formatted .= intval($this->month);
          break;
        case 't':
          $formatted .= $this->isLeap && $this->month == 12 ? 30 : $month_days[$this->month - 1];
          break;
        case 'L':
          $formatted .= $this->isLeap ? 1 : 0;
          break;
        case 'Y':
          $formatted .= $this->year;
          break;
        case 'y':
          $formatted .= substr($this->year, 2, 4);
          break;
        case 'o':
          $formatted .= $this->year;
          break;
        case 'a':
          $formatted .= $t($names['ampm'][$ampm]);
          break;
        case 'A':
          $formatted .= $t($names['ampm'][$ampm]);
          break;
        case 'c':
          $formatted .= "{$this->year} - {$this->month} - {$this->day}T";
          $formatted .= $this
            ->xformat('H:i:sP');
          break;
        case 'r':
          $formatted .= $t($names['day_abbr'][$fw]) . ', ' . $cd . ' ' . $t($names['months'][$cm]) . ' ' . $cy . $this
            ->xformat('H:i:s P');
          break;
        default:

          // Any format charachter not handled by Datex or extended class,
          // Will be handled by DateTime.
          $formatted .= ctype_alpha($format[$i]) ? $this
            ->xformat($format[$i]) : $format[$i];
          break;
      }
    }
  }
  return $formatted;
}