You are here

public static function DatexFormatter::toJalali in Datex 7

Converts a Gregorian date to Jalali.

5 calls to DatexFormatter::toJalali()
DatexFormatter::formatPHP in datex_api/datex_api_classes.inc
Formats a date according to format given.
DatexFormatter::toGregorian in datex_api/datex_api_classes.inc
Converts a Jalali date to Gregorian.
DatexObjectUtils::getByName in datex_api/datex_api_classes.inc
Get current part of date as specified in name. Any of day, month, year.
DatexObjectUtils::getdate in datex_api/datex_api_classes.inc
See php getdate().
datex_date_date_select_process_alter in datex_date/datex_date.module
Implements hook date_select_process_alter().

File

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

Class

DatexFormatter
Date tools for Jalali Dates.

Code

public static function toJalali($gregorian_year = 0, $gregorian_month = 0, $gregorian_day = 0) {
  $now = getdate();
  $gregorian_year = ($gregorian_year ? $gregorian_year : $now['year']) - 1600;
  $gregorian_month = ($gregorian_month ? $gregorian_month : $now['mon']) - 1;
  $gregorian_day = ($gregorian_day ? $gregorian_day : $now['mday']) - 1;
  $gregorian_day_no = 365 * $gregorian_year + intval(($gregorian_year + 3) / 4) - intval(($gregorian_year + 99) / 100) + intval(($gregorian_year + 399) / 400);
  for ($i = 0; $i < $gregorian_month; ++$i) {
    $gregorian_day_no += self::$daysInGregorianMonth[$i];
  }
  if ($gregorian_month > 1 && ($gregorian_year % 4 == 0 && $gregorian_year % 100 != 0) || $gregorian_year % 400 == 0) {

    // Leap and after Feb.
    $gregorian_day_no++;
  }
  $gregorian_day_no += $gregorian_day;
  $jalali_day_no = $gregorian_day_no - 79;
  $j_np = intval($jalali_day_no / 12053);
  $jalali_day_no = $jalali_day_no % 12053;
  $j_year = 979 + 33 * $j_np + 4 * intval($jalali_day_no / 1461);
  $jalali_day_no %= 1461;
  if ($jalali_day_no >= 366) {
    $j_year += intval(($jalali_day_no - 1) / 365);
    $jalali_day_no = ($jalali_day_no - 1) % 365;
  }
  for ($i = 0; $i < 11 && $jalali_day_no >= self::$daysInJalaliMonth[$i]; ++$i) {
    $jalali_day_no -= self::$daysInJalaliMonth[$i];
  }
  $j_month = $i + 1;
  $j_day = $jalali_day_no + 1;
  return array(
    'day' => $j_day,
    'month' => $j_month,
    'year' => $j_year,
  );
}