Skip to content

Instantly share code, notes, and snippets.

@faizshukri
Last active August 10, 2021 05:03
Show Gist options
  • Save faizshukri/7804628 to your computer and use it in GitHub Desktop.
Save faizshukri/7804628 to your computer and use it in GitHub Desktop.

Convert gregorian date to Hijri

####How to use By default, we create the Hijri object with the current time

$hijri = new HijriDate();

We can set a specific time in the constructor

//passing time
$hijri = new HijriDate( 580060800 );

//or just
$hijri = new HijriDate( strtotime('1998-05-20') );

Then we can retrieve the date with

$hijri->get_date(); //3 Syawal 1408H

Should we want the specific information, we can retrieve it using

$hijri->get_day(); //3
$hijri->get_month(); //10
$hijri->get_year(); //1408

$hijri->get_month_name( 10 ); //syawal
//or from the date given
$hijri->get_month_name( $hijri->get_month() ); //syawal

####License

Released under the MIT license

####Credits to

This code was the improved version created by me, and originally written by amichauer at gmx dot de ¶ guy at PHP.NET

<?php
/**
* Get hijri date from gregorian
*
* @author Faiz Shukri
* @date 5 Dec 2013
* @url https://gist.github.com/faizshukri/7802735
*
* Copyright 2013 | Faiz Shukri
* Released under the MIT license
*/
class HijriDate{
private $hijri;
public function __construct( $time = false ){
if(!$time) $time = time();
$this->hijri = $this->GregorianToHijri($time);
}
public function get_date(){
return $this->hijri[1] . ' ' . $this->get_month_name($this->hijri[0]) . ' ' . $this->hijri[2] . 'H';
}
public function get_day(){
return $this->hijri[1];
}
public function get_month(){
return $this->hijri[0];
}
public function get_year(){
return $this->hijri[2];
}
public function get_month_name($i){
static $month = array(
"muharram", "safar", "rabiulawal", "rabiulakhir",
"jamadilawal", "jamadilakhir", "rejab", "syaaban",
"ramadhan", "syawal", "zulkaedah", "zulhijjah"
);
return $month[$i-1];
}
private function GregorianToHijri($time = null){
if ($time === null) $time = time();
$m = date('m', $time);
$d = date('d', $time);
$y = date('Y', $time);
return $this->JDToHijri(cal_to_jd(CAL_GREGORIAN, $m, $d, $y));
}
private function HijriToGregorian($m, $d, $y){
return jd_to_cal(CAL_GREGORIAN, $this->HijriToJD($m, $d, $y));
}
# Julian Day Count To Hijri
private function JDToHijri($jd){
$jd = $jd - 1948440 + 10632;
$n = (int)(($jd - 1) / 10631);
$jd = $jd - 10631 * $n + 354;
$j = ((int)((10985 - $jd) / 5316)) *
((int)(50 * $jd / 17719)) +
((int)($jd / 5670)) *
((int)(43 * $jd / 15238));
$jd = $jd - ((int)((30 - $j) / 15)) *
((int)((17719 * $j) / 50)) -
((int)($j / 16)) *
((int)((15238 * $j) / 43)) + 29;
$m = (int)(24 * $jd / 709);
$d = $jd - (int)(709 * $m / 24);
$y = 30*$n + $j - 30;
return array($m, $d, $y);
}
# Hijri To Julian Day Count
private function HijriToJD($m, $d, $y){
return (int)((11 * $y + 3) / 30) +
354 * $y + 30 * $m -
(int)(($m - 1) / 2) + $d + 1948440 - 385;
}
}
@uzielweb
Copy link

Call to undefined function cal_to_jd()

@andrewwilsonofficial
Copy link

Call to undefined function cal_to_jd()

@faizshukri
Copy link
Author

cal_to_jd is php-calendar function. make sure your php has that module installed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment