You are here

public static function DatexFormatter::formatPHP in Datex 7

Formats a date according to format given.

This function uses internal methods for converting, See DatexFormatter::formatINTL is suggested instead.

2 calls to DatexFormatter::formatPHP()
DatexFormatter::format in datex_api/datex_api_classes.inc
Similar to php date_format.
DatexObjectUtils::getdate in datex_api/datex_api_classes.inc
See php getdate().

File

datex_api/datex_api_classes.inc, line 277
API and helper functions used by other datex modules.

Class

DatexFormatter
Date tools for Jalali Dates.

Code

public static function formatPHP($date, $format, $tz) {
  $persian_date_names = self::persianDateNames();

  // For anyone reading this comment: Passing a DateTimeZone to datetime
  // constructor has no effect on it! You MUST use setTimezone to set a
  // tz on the stupid object.
  // Tested on PHP 5.4.15 (built: May 12 2013 13:11:23) Archlinux.
  $date = self::ObjectFromDate($date);
  if ($tz) {
    $tz = self::getTzObject($tz);
    $date
      ->setTimezone($tz);
  }
  $gregorian_date = array(
    'd' => intval($date
      ->format('j')),
    'm' => intval($date
      ->format('n')),
    'Y' => intval($date
      ->format('Y')),
  );
  $jalali_date = self::toJalali($gregorian_date['Y'], $gregorian_date['m'], $gregorian_date['d']);
  $z = $jalali_date['month'] <= 6 ? $jalali_date['month'] * 31 + $jalali_date['day'] : 186 + (($jalali_date['month'] - 6) * 30 + $jalali_date['day']);
  $format = preg_replace('/[\\\\][a-z]/', '', $format);
  $is_leap = self::isLeap($jalali_date['year']) ? 1 : 0;
  $replacements = array(
    'd' => sprintf('%02d', $jalali_date['day']),
    'D' => $persian_date_names['day_abbr'][$date
      ->format('N')],
    'j' => $jalali_date['day'],
    'l' => $persian_date_names['day'][$date
      ->format('N')],
    'S' => $persian_date_names['day_abbr'][$date
      ->format('N')],
    'F' => $persian_date_names['months'][$jalali_date['month']],
    'm' => sprintf('%02d', $jalali_date['month']),
    'n' => $jalali_date['month'],
    'L' => str_replace('L', $is_leap, $format),
    'Y' => $jalali_date['year'],
    'y' => $jalali_date['year'],
    'o' => $jalali_date['year'],
    'a' => $persian_date_names['ampm'][$date
      ->format('a')],
    'A' => $persian_date_names['ampm'][$date
      ->format('a')],
    'B' => $persian_date_names['ampm'][$date
      ->format('a')],
    'c' => $jalali_date['year'] . '-' . $jalali_date['month'] . '-' . $jalali_date['day'] . 'T' . $date
      ->format('H:i:sP'),
    'g' => $date
      ->format('g'),
    'G' => $date
      ->format('G'),
    'h' => $date
      ->format('h'),
    'H' => $date
      ->format('H'),
    'i' => $date
      ->format('i'),
    's' => $date
      ->format('s'),
    'u' => $date
      ->format('u'),
    'I' => $date
      ->format('I'),
    'O' => $date
      ->format('O'),
    'P' => $date
      ->format('P'),
    'T' => $date
      ->format('T'),
    'Z' => $date
      ->format('Z'),
    'U' => $date
      ->format('U'),
    'w' => $date
      ->format('w'),
    'N' => $date
      ->format('N'),
    'e' => $date
      ->format('e'),
    'z' => $z,
    'W' => ceil($z / 7),
    't' => self::$daysInJalaliMonth[$jalali_date['month'] - 1],
  );
  $replacements['r'] = $persian_date_names['day_abbr'][$date
    ->format('N')] . ', ' . $jalali_date['day'] . ' ' . $persian_date_names['months'][$jalali_date['month']] . ' ' . $jalali_date['year'] . $date
    ->format('H:i:s P');
  if ($is_leap && $jalali_date['month'] == 12) {
    $replacements['t'] = 30;
  }
  else {
    $replacements['t'] = self::$daysInJalaliMonth[$jalali_date['month'] - 1];
  }
  return strtr($format, $replacements);
}