You are here

function hijri in Hijri 7

Same name and namespace in other branches
  1. 8 hijri.module \hijri()

Retrive Hijri date from given format and timestamp.

2 calls to hijri()
hijri_format_date in ./hijri.module
@file Retrive Hijri date like Drupal format_date function.
hijri_settings_form in ./hijri.module
Form builder.
5 string references to 'hijri'
HijriTestCase::setUp in tests/hijri.test
Implements setUp().
hijri_block_view in ./hijri.module
Implements hook_block_view().
hijri_theme in ./hijri.module
Implements hook_theme().
hijri_views_api in ./hijri.module
Implements hook_views_api().
hijri_views_handlers in views/hijri.views.inc
Implements hook_views_handlers().

File

./hijri.module, line 438
This module convert to Hijri date in nodes,comments and a block.

Code

function hijri($format, $timestamp = NULL, $correction = 0) {
  $timestamp = $timestamp == NULL ? time() : $timestamp;
  $patterns = array();
  $replacements = array();
  array_push($patterns, 'Y');
  array_push($replacements, 'x1');
  array_push($patterns, 'y');
  array_push($replacements, 'x2');
  array_push($patterns, 'M');
  array_push($replacements, 'x3');
  array_push($patterns, 'F');
  array_push($replacements, 'x3');
  array_push($patterns, 'n');
  array_push($replacements, 'x4');
  array_push($patterns, 'm');
  array_push($replacements, 'x5');
  array_push($patterns, 'j');
  array_push($replacements, 'x6');
  array_push($patterns, 'd');
  array_push($replacements, 'x7');
  array_push($patterns, 'S');
  array_push($replacements, '');
  $format = str_replace($patterns, $replacements, $format);

  // 1. Converting user given date before any increment.
  $date_result = format_date($timestamp, 'custom', $format);

  // 2. Preparing timestamp if there is increments?
  if ($correction != 0) {
    $timestamp = $timestamp + 60 * 60 * 24 * $correction;
  }

  // 3. $calc_hijri_date to retrive Hijri Year.
  // Hijri Month and Hijri day in numbers.
  $year = format_date($timestamp, 'custom', 'Y');
  $month = format_date($timestamp, 'custom', 'n');
  $day = format_date($timestamp, 'custom', 'j');
  $calc_hijri_date = "";
  $hijri_month_name = "";
  switch (variable_get('hijri_datetype_value')) {
    case 'hijir_solar':
      $calc_hijri_date = hijri_shamsi_calc($year, $month, $day);
      $hijri_month_name = hijri_shamsi_month_names();
      break;
    default:
    case 'hijir_moon':
      $calc_hijri_date = hijri_calc($year, $month, $day);
      $hijri_month_name = hijri_month_names();
      break;
  }

  // 4. Replacing reserved constants with Hijri results.
  $patterns = $replacements = array();

  // Y.
  $patterns[] = 'x1';
  $replacements[] = $calc_hijri_date[0];

  // Y.
  $patterns[] = 'x2';
  $replacements[] = substr($calc_hijri_date[0], -2);

  // M .. There is no shortname in Hijri month names.
  $patterns[] = 'x3';
  $replacements[] = $hijri_month_name[$calc_hijri_date[1]];

  // N.
  $patterns[] = 'x4';
  $replacements[] = $calc_hijri_date[1];

  // M.
  $patterns[] = 'x5';
  $replacements[] = sprintf('%02d', $calc_hijri_date[1]);

  // J.
  $patterns[] = 'x6';
  $replacements[] = $calc_hijri_date[2];

  // D.
  $patterns[] = 'x7';
  $replacements[] = sprintf('%02d', $calc_hijri_date[2]);
  return str_replace($patterns, $replacements, $date_result);
}