How can I convert Hijri date to gregorian in PHP?
Asked Answered
M

3

9

I would like convert this date:09/13/1436 (it is Hijiri) to 2015-06-30(it is Gregorian). I tried that:

function HijriToJD($m, $d, $y){
   return (int)((11 * $y + 3) / 30) + 354 * $y + 
     30 * $m - (int)(($m - 1) / 2) + $d + 1948440 - 385;
}


$date = HijriToJD(09, 13, 1436);

echo jdtogregorian($date);

and when i made compile i got 10/7/2014. Someone have any idea??

Meticulous answered 1/7, 2015 at 9:20 Comment(7)
maybe take a look at php.net/manual/de/ref.calendar.php Comment #4Germanous
can you tell what is date and month in 09/13/1436?Kaddish
09 - > month,13->day,1436->year.Meticulous
how to convert hijri date to gregorian date using phpNeuritis
I made convert on website ,so i know it is must be 2015-06-30. So i dont know why i got 10/7/2014.Meticulous
@john-conde - not really a duplicate of that John.... poster is having problems converting dates between calendars, not simply formatsNeuritis
@MarkBaker Fair enough. Reopened.Yapon
N
8

Passing 09 as the month is the problem.... a number with a leading zero is treated as octal in PHP. 09 is invalid octal, so it is treated as a 0.

Calling

$r=HijriToJD(9, 13, 1436);

(without the leading zero for the month) should give you a correct result

Neuritis answered 1/7, 2015 at 9:42 Comment(2)
Yes!! Thanks you are my Hero.Meticulous
I couldn't let you get the month wrong at such an important time of year could I? :)Neuritis
K
1

More accurate will be

return floor((11 * $year + 3) / 30) + floor(354 * $year) + floor(30 * $month)
            - floor(($month - 1) / 2) + $day + 1948440 - 386;

You can use the floor instead of int

https://github.com/OpenSID/OpenSID/blob/master/donjo-app/libraries/Date_conv.php#L44

Koehn answered 24/10, 2019 at 11:17 Comment(0)
K
-2
function HijriToJD($m, $d, $y){
    $m= intval($m); $d= intval($d); $y= intval($y);
    
       return (int)((11 * $y + 3) / 30) + 354 * $y + 
         30 * $m - (int)(($m - 1) / 2) + $d + 1948440 - 385;
}
    
$date = HijriToJD(09, 13, 1436);

echo jdtogregorian($date);
Kianakiang answered 7/6 at 23:9 Comment(2)
Code-only answers are not good answersSabbatarian
What did you change, compared to the original code, and why?London

© 2022 - 2024 — McMap. All rights reserved.