You are here

private static function DatexPoorMansJaliliCalendar::toJalali in Datex 8

Converts gregorian date to jalali date.

by Sallar Kaboli

1 call to DatexPoorMansJaliliCalendar::toJalali()
DatexPoorMansJaliliCalendar::_format in src/Datex/DatexPoorMansJaliliCalendar.php

File

src/Datex/DatexPoorMansJaliliCalendar.php, line 334
Fallback calendar implementation in case php-intl is not avaiable.

Class

DatexPoorMansJaliliCalendar
Jalali calendar for datex.

Namespace

Drupal\datex\Datex

Code

private static function toJalali($g_y, $g_m, $g_d) {
  $g_days_in_month = [
    31,
    28,
    31,
    30,
    31,
    30,
    31,
    31,
    30,
    31,
    30,
    31,
  ];
  $j_days_in_month = [
    31,
    31,
    31,
    31,
    31,
    31,
    30,
    30,
    30,
    30,
    30,
    29,
  ];
  $gy = $g_y - 1600;
  $gm = $g_m - 1;
  $gd = $g_d - 1;
  $g_day_no = 365 * $gy + self::div($gy + 3, 4) - self::div($gy + 99, 100) + self::div($gy + 399, 400);
  for ($i = 0; $i < $gm; ++$i) {
    $g_day_no += $g_days_in_month[$i];
  }
  if ($gm > 1 && ($gy % 4 == 0 && $gy % 100 != 0 || $gy % 400 == 0)) {
    $g_day_no++;
  }
  $g_day_no += $gd;
  $j_day_no = $g_day_no - 79;
  $j_np = self::div($j_day_no, 12053);
  $j_day_no = $j_day_no % 12053;
  $jy = 979 + 33 * $j_np + 4 * self::div($j_day_no, 1461);
  $j_day_no %= 1461;
  if ($j_day_no >= 366) {
    $jy += self::div($j_day_no - 1, 365);
    $j_day_no = ($j_day_no - 1) % 365;
  }
  for ($i = 0; $i < 11 && $j_day_no >= $j_days_in_month[$i]; ++$i) {
    $j_day_no -= $j_days_in_month[$i];
  }
  $jm = $i + 1;
  $jd = $j_day_no + 1;
  return [
    $jy,
    $jm,
    $jd,
  ];
}