You are here

public static function DatexJalali::toGregorian in Datex 7.2

Converts a Jalali date to Gregorian.

@TODO replcae with khayam calcualtion method.

Overrides DatexCalendarIterface::toGregorian

File

datex_api/DatexJalali.inc, line 371
Jalali calendar for datex.

Class

DatexJalali
Jalali calendar for datex.

Code

public static function toGregorian($jalali_year = 0, $jalali_month = 0, $jalali_day = 0) {
  $now = self::convert(time());
  $jalali_year = ($jalali_year ? $jalali_year : $now[0]) - 979;
  $jalali_month = ($jalali_month ? $jalali_month : $now[1]) - 1;
  $jalali_day = ($jalali_day ? $jalali_day : $now[2]) - 1;
  $jalali_day_no = 365 * $jalali_year + intval($jalali_year / 33) * 8 + intval(($jalali_year % 33 + 3) / 4);
  for ($i = 0; $i < $jalali_month; ++$i) {
    $jalali_day_no += self::$monthDays[$i];
  }
  $jalali_day_no += $jalali_day;
  $gregorian_day_no = $jalali_day_no + 79;
  $g_year = 1600 + 400 * intval($gregorian_day_no / 146097);
  $gregorian_day_no = $gregorian_day_no % 146097;
  $leap = TRUE;
  if ($gregorian_day_no >= 36525) {
    $gregorian_day_no--;
    $g_year += 100 * intval($gregorian_day_no / 36524);
    $gregorian_day_no = $gregorian_day_no % 36524;
    if ($gregorian_day_no >= 365) {
      $gregorian_day_no++;
    }
    else {
      $leap = FALSE;
    }
  }
  $g_year += 4 * intval($gregorian_day_no / 1461);
  $gregorian_day_no %= 1461;
  if ($gregorian_day_no >= 366) {
    $leap = FALSE;
    $gregorian_day_no--;
    $g_year += intval($gregorian_day_no / 365);
    $gregorian_day_no = $gregorian_day_no % 365;
  }
  for ($i = 0; $gregorian_day_no >= self::$daysInGregorianMonth[$i] + ($i == 1 && $leap); $i++) {
    $gregorian_day_no -= self::$daysInGregorianMonth[$i] + ($i == 1 && $leap);
  }
  $g_month = $i + 1;
  $g_day = $gregorian_day_no + 1;
  return array(
    $g_year,
    $g_month,
    $g_day,
  );
}