You are here

public static function DatexFormatter::toGregorian in Datex 7

Converts a Jalali date to Gregorian.

4 calls to DatexFormatter::toGregorian()
DatexFormatter::ObjectFromDate in datex_api/datex_api_classes.inc
Created a DateTime object containing date from $date.
DatexObject::setDate in datex_api/datex_api_classes.inc
Sets date and time zone.
DatexObjectUtils::mktime in datex_api/datex_api_classes.inc
See php mktime().
_datex_date_element_validate_callback in datex_date/datex_date.module
Form element validation callback.

File

datex_api/datex_api_classes.inc, line 157
API and helper functions used by other datex modules.

Class

DatexFormatter
Date tools for Jalali Dates.

Code

public static function toGregorian($jalali_year = 0, $jalali_month = 0, $jalali_day = 0) {
  $now = self::toJalali();
  $jalali_year = ($jalali_year ? $jalali_year : $now['year']) - 979;
  $jalali_month = ($jalali_month ? $jalali_month : $now['month']) - 1;
  $jalali_day = ($jalali_day ? $jalali_day : $now['day']) - 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::$daysInJalaliMonth[$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(
    'year' => $g_year,
    'month' => $g_month,
    'day' => $g_day,
  );
}