public function HijriFormatter::hijri in Hijri 1.0.x
Same name and namespace in other branches
- 8.2 src/HijriFormatter.php \Drupal\hijri\HijriFormatter::hijri()
- 3.0.x src/HijriFormatter.php \Drupal\hijri\HijriFormatter::hijri()
Retrive Hijri date from given format and timestamp.
Parameters
$format:
null $timestamp:
int $correction:
Return value
mixed
1 call to HijriFormatter::hijri()
- HijriFormatter::format in src/
HijriFormatter.php - Formats a date, using a date type or a custom date format string.
File
- src/
HijriFormatter.php, line 160
Class
- HijriFormatter
- Provides a service to handle Hijri date related functionality.
Namespace
Drupal\hijriCode
public function hijri($format, $timestamp = NULL, $correction = 0) {
$timestamp = $timestamp == NULL ? time() : $timestamp;
$patterns = [];
$replacements = [];
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 = \Drupal::service('date.formatter')
->format($timestamp, 'custom', $format);
// 2. Preparing timestamp if there is increments?
if ($correction != 0) {
$timestamp = $timestamp + 60 * 60 * 24 * $correction;
}
// $calc_hijri_date to retrive Hijri Year.
// Hijri Month and Hijri day in numbers.
list($y, $m, $d) = explode(' ', \Drupal::service('date.formatter')
->format($timestamp, 'custom', 'Y m d'));
$calc_hijri_date = (new Date())
->hjConvert($y, $m, $d);
$hijri_month_name = $this
->hijriMonthNames();
// 4. Replacing reserved constants with Hijri results.
$patterns = [];
$replacements = [];
// 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);
}