You are here

function calendar_jalali_formatter in Calendar Systems 6.2

Jalali calendar date formatter callback.

{Calendar conversion is a just a pain!}

Parameters

$timestamp: Unix timestamp to be formatted. The proper $timezone value has been added by Drupal format_date().

$format: PHP date() function format string.

$timezone: Time zone offset in seconds.

$langcode: Optional language code to translate to a language other than the default.

$calendar: A copy of calendar information.

Return value

The formatted date or FALSE otherwise.

See also

format_date()

1 string reference to 'calendar_jalali_formatter'
calendar_jalali_calendar_info in calendars/calendar_jalali/calendar_jalali.module
Implements hook_calendar_info().

File

calendars/calendar_jalali/calendar_jalali.module, line 260
Implements necessary hooks & helpers to support Jalali Calendar.

Code

function calendar_jalali_formatter($timestamp, $format, $timezone = 0, $langcode = NULL, $calendar = NULL) {
  $date = '';
  list($year, $month, $day) = explode('~', calendar_systems_formatter($timestamp, 'Y~n~j'));
  list($jyear, $jmonth, $jday) = calendar_jalali_convert($year, $month, $day);
  $max = strlen($format);
  for ($i = 0; $i < $max; $i++) {
    switch ($format[$i]) {
      case 'y':
        $date .= substr($jyear, 2, 4);
        break;
      case 'Y':
        $date .= $jyear;
        break;
      case 'M':
      case 'F':

        // Check configs for month representation.
        $function = $calendar['config']['calendar_jalali_translate_months'] ? 'array_values' : 'array_keys';
        $jmonths = $function(_calendar_jalali_t('months'));
        $date .= $jmonths[$jmonth - 1];
        break;
      case 'D':
      case 'l':
        $function = $calendar['config']['calendar_jalali_translate_weekdays'] ? 'array_values' : 'array_keys';
        $weekdays = $function(_calendar_jalali_t('weekdays'));
        $date .= $weekdays[calendar_jalali_formatter($timestamp, 'w')];
        break;
      case 'a':
      case 'A':
        $default = calendar_systems_formatter($timestamp, $format[$i]);
        if ($calendar['config']['calendar_jalali_translate_meridiem']) {
          $case = $format[$i] == 'a' ? 'lower' : 'upper';
          $meridiems = _calendar_jalali_t('meridiem_' . $case);
          $date .= $meridiems[$default];
        }
        else {
          $date .= $default;
        }
        break;
      case 'm':
        $date .= sprintf('%02d', $jmonth);
        break;
      case 'n':
        $date .= $jmonth;
        break;
      case 'd':
        $date .= sprintf('%02d', $jday);
        break;
      case 'j':
        $date .= $jday;
        break;
      case 'w':
        $date .= (calendar_systems_formatter($timestamp, 'w') + 1) % 7;
        break;
      case 'r':
        $date .= calendar_jalali_formatter($timestamp, 'D, d M Y H:i:s O', $timezone, NULL, $calendar);
        break;
      case 'N':
        $date .= calendar_jalali_formatter($timestamp, 'w') + 1;
        break;
      case 't':
        $jmonth_days = calendar_jalali_month_days();
        if ($jmonth < 12) {
          $date .= $jmonth_days[$jmonth - 1];
        }
        elseif (_calendar_jalali_check($jyear, $jmonth, 30)) {
          $date .= '30';
        }
        else {
          $date .= '29';
        }
        break;
      case 'z':
        $day_of_year = 0;
        $jmonth_days = calendar_jalali_month_days();
        for ($n = 0; $n < $jmonth - 1; $n++) {
          $day_of_year += $jmonth_days[$n];
        }
        $day_of_year += $jday - 1;
        $date .= $day_of_year;
        break;
      case 'L':
        $date .= _calendar_jalali_check($jyear, 12, 30) ? '1' : '0';
        break;
      case 'W':
        $zone = calendar_jalali_formatter($timestamp, 'z');

        // First saturday.
        $saturday = ($zone - calendar_jalali_formatter($timestamp, 'w') + 7) % 7;
        $days = $zone - $saturday;
        if ($days < 0) {
          $zone += _calendar_jalali_check($jyear - 1, 12, 30) ? 366 : 365;
          $saturday = ($zone - calendar_jalali_formatter($timestamp, 'w') + 7) % 7;
          $days = $zone - $saturday;
        }
        $date .= floor($days / 7) + 1;
        break;
      case '\\':
        if ($i + 1 < strlen($format)) {
          $date .= $format[++$i];
        }
        else {
          $date .= $format[$i];
        }
        break;
      default:
        $date .= calendar_systems_formatter($timestamp, $format[$i], $timezone);
    }
  }

  // Check configs for number representation.
  if ($calendar['config']['calendar_jalali_translate_numbers']) {
    return _calendar_jalali_convert_number($date);
  }
  return $date;
}