You are here

public static function jDateTime::date in Persian Date for Drupal 8 8.4

Parameters

$format:

bool $stamp:

bool $timezone:

Return value

mixed

1 call to jDateTime::date()
jDateTime::strftime in src/Library/Jalali/jDateTime.php

File

src/Library/Jalali/jDateTime.php, line 326

Class

jDateTime
Class jDateTime @package Morilog\Jalali

Namespace

Drupal\persian_date\Library\Jalali

Code

public static function date($format, $stamp = false, $timezone = null) {
  $stamp = $stamp !== false ? $stamp : time();
  $dateTime = static::createDateTime($stamp, $timezone);

  //Find what to replace
  $chars = preg_match_all('/([a-zA-Z]{1})/', $format, $chars) ? $chars[0] : array();

  //Intact Keys
  $intact = array(
    'B',
    'h',
    'H',
    'g',
    'G',
    'i',
    's',
    'I',
    'U',
    'u',
    'Z',
    'O',
    'P',
  );
  $intact = self::filterArray($chars, $intact);
  $intactValues = array();
  foreach ($intact as $k => $v) {
    $intactValues[$k] = $dateTime
      ->format($v);
  }

  //End Intact Keys

  //Changed Keys
  list($year, $month, $day) = array(
    $dateTime
      ->format('Y'),
    $dateTime
      ->format('n'),
    $dateTime
      ->format('j'),
  );
  list($jYear, $jMonth, $jDay) = self::toJalali($year, $month, $day);
  $keys = array(
    'd',
    'D',
    'j',
    'l',
    'N',
    'S',
    'w',
    'z',
    'W',
    'F',
    'm',
    'M',
    'n',
    't',
    'L',
    'o',
    'Y',
    'y',
    'a',
    'A',
    'c',
    'r',
    'e',
    'T',
  );
  $keys = self::filterArray($chars, $keys, array(
    'z',
  ));
  $values = array();
  foreach ($keys as $k => $key) {
    $v = '';
    switch ($key) {

      //Day
      case 'd':
        $v = sprintf("%02d", $jDay);
        break;
      case 'D':
        $v = self::getDayNames($dateTime
          ->format('D'), true);
        break;
      case 'j':
        $v = $jDay;
        break;
      case 'l':
        $v = self::getDayNames($dateTime
          ->format('l'));
        break;
      case 'N':
        $v = self::getDayNames($dateTime
          ->format('l'), false, 1, true);
        break;
      case 'S':
        $v = 'ام';
        break;
      case 'w':
        $v = self::getDayNames($dateTime
          ->format('l'), false, 1, true) - 1;
        break;
      case 'z':
        if ($jMonth > 6) {
          $v = 186 + ($jMonth - 6 - 1) * 30 + $jDay;
        }
        else {
          $v = ($jMonth - 1) * 31 + $jDay;
        }
        self::$temp['z'] = $v;
        break;

      //Week
      case 'W':
        $v = is_int(self::$temp['z'] / 7) ? self::$temp['z'] / 7 : intval(self::$temp['z'] / 7 + 1);
        break;

      //Month
      case 'F':
        $v = self::getMonthNames($jMonth);
        break;
      case 'm':
        $v = sprintf("%02d", $jMonth);
        break;
      case 'M':
        $v = self::getMonthNames($jMonth, true);
        break;
      case 'n':
        $v = $jMonth;
        break;
      case 't':
        $v = $jMonth == 12 ? 29 : ($jMonth > 6 && $jMonth != 12 ? 30 : 31);
        break;

      //Year
      case 'L':
        $tmpObj = static::createDateTime(time() - 31536000, $timezone);
        $v = $tmpObj
          ->format('L');
        break;
      case 'o':
      case 'Y':
        $v = $jYear;
        break;
      case 'y':
        $v = $jYear % 100;
        break;

      //Time
      case 'a':
        $v = $dateTime
          ->format('a') == 'am' ? 'ق.ظ' : 'ب.ظ';
        break;
      case 'A':
        $v = $dateTime
          ->format('A') == 'AM' ? 'قبل از ظهر' : 'بعد از ظهر';
        break;

      //Full Dates
      case 'c':
        $v = $jYear . '-' . sprintf("%02d", $jMonth) . '-' . sprintf("%02d", $jDay) . 'T';
        $v .= $dateTime
          ->format('H') . ':' . $dateTime
          ->format('i') . ':' . $dateTime
          ->format('s') . $dateTime
          ->format('P');
        break;
      case 'r':
        $v = self::getDayNames($dateTime
          ->format('D'), true) . ', ' . sprintf("%02d", $jDay) . ' ' . self::getMonthNames($jMonth, true);
        $v .= ' ' . $jYear . ' ' . $dateTime
          ->format('H') . ':' . $dateTime
          ->format('i') . ':' . $dateTime
          ->format('s') . ' ' . $dateTime
          ->format('P');
        break;

      //Timezone
      case 'e':
        $v = $dateTime
          ->format('e');
        break;
      case 'T':
        $v = $dateTime
          ->format('T');
        break;
    }
    $values[$k] = $v;
  }

  //End Changed Keys

  //Merge
  $keys = array_merge($intact, $keys);
  $values = array_merge($intactValues, $values);
  return strtr($format, array_combine($keys, $values));
}