1

I want to convert hijri date to gregorian date vice versa using php. I'm trying this code

<?php

class HijriCalendar
{
 function GregorianToHijri($time = null)
    {
        if ($time === null) $time = time();

        $m = date('m', $time);
        $d = date('d', $time);
        $y = date('Y', $time);

        return HijriCalendar::JDToHijri(
            cal_to_jd(CAL_GREGORIAN, $m, $d, $y));
    }

    function HijriToGregorian($time = null)
    {   
        if ($time === null) $time = time();
        $m = date('m', $time);
        $d = date('d', $time);
        $y = date('Y', $time);

          return jdtogregorian(HijriCalendar::HijriToJD($m, $d, $y));
    }

    # Julian Day Count To Hijri
    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
    function HijriToJD($m, $d, $y)
    {
        return (int)((11 * $y + 3) / 30) +
            354 * $y + 30 * $m -
            (int)(($m - 1) / 2) + $d + 1948440 - 385;
    }
};
 $date=time('1439-05-27');
 $hijri = HijriCalendar::HijriToGregorian(time($date));
 print_r($hijri)

?>

but this return wrong hijri date to gregorian date but gregorian to hijri date return correct date input but

'1439-05-27'

output

9/8/2579

but i expected date is 2018-03-15 how can I fix this ?? and I want to convert date vice versa

Cœur
  • 37,241
  • 25
  • 195
  • 267
naseeba c
  • 1,040
  • 2
  • 14
  • 30

2 Answers2

0

The problem is in passing the values with the leading zero.

$m = date('m', $time);
$d = date('d', $time);
$y = date('Y', $time);

Change as below

$m = date('n', $time);
$d = date('j', $time);
$y = date('Y', $time);
Nandhini
  • 54
  • 7
0

replace your code after the class with :

$j=HijriCalendar::HijritoJD(5,27,1439);
$g=jdtogregorian($j);
print_r($g);

this will convert Hijri date to Julianday. After that convert Julianday to Gregorian date.

you can also use HijriDateLib. it is well documented.

hubaishan
  • 51
  • 1
  • 1
  • 6