You are here

static function PersianDateConverter::gregorian_to_jalali in Persian Date for Drupal 8 8.4

Same name and namespace in other branches
  1. 8 src/Converter/PersianDateConverter.php \Drupal\persian_date\Converter\PersianDateConverter::gregorian_to_jalali()

Convert Gregorian date parameters to Persian.

Parameters

int $year Gregorian year:

int $month Gregorian month:

int $day Gregorian day:

string $mod:

Return value

array|string

1 call to PersianDateConverter::gregorian_to_jalali()
PersianDate::format in src/Converter/PersianDate.php
@inheritDoc

File

src/Converter/PersianDateConverter.php, line 33

Class

PersianDateConverter
This class converts Gregorian datetime to Persian datetime and reverse.

Namespace

Drupal\persian_date\Converter

Code

static function gregorian_to_jalali($year, $month, $day, $mod = '') {
  $g_d_m = array(
    0,
    31,
    59,
    90,
    120,
    151,
    181,
    212,
    243,
    273,
    304,
    334,
  );
  $jy = $year <= 1600 ? 0 : 979;
  $year -= $year <= 1600 ? 621 : 1600;
  $gy2 = $month > 2 ? $year + 1 : $year;
  $days = 365 * $year + (int) (($gy2 + 3) / 4) - (int) (($gy2 + 99) / 100) + (int) (($gy2 + 399) / 400) - 80 + $day + $g_d_m[$month - 1];
  $jy += 33 * (int) ($days / 12053);
  $days %= 12053;
  $jy += 4 * (int) ($days / 1461);
  $days %= 1461;
  $jy += (int) (($days - 1) / 365);
  if ($days > 365) {
    $days = ($days - 1) % 365;
  }
  $jm = $days < 186 ? 1 + (int) ($days / 31) : 7 + (int) (($days - 186) / 30);
  $jd = 1 + ($days < 186 ? $days % 31 : ($days - 186) % 30);
  return $mod == '' ? array(
    $jy,
    $jm,
    $jd,
  ) : $jy . $mod . $jm . $mod . $jd;
}