Coding in PHP a Hijri-Gregorian Calendar

Why a Hijri-Gregorian calendar:

Muslims follow a lunar calendar, named Hijri calendar. Hijra means “migration” in arabic. The muslim lunar calendar starts with the migration of the prophet Muhammad, peace be upon him, from Mecca to Medina. The actual muslim hijri year is 1431.

The actual civilian solar calendar, used throughout the world, is called Gregorian. Pope Gregory XIII introduced the calendar by a decree dated 24 February 1582.

The lunar month has 29 or 30 days. The lunar year has 354 or 355 days and effectively “fluctuates” backwards direction (10 or 11 days) in actual solar year. Muslim lunar calendar is quite convenient as, for example, the  month of fasting, Ramadhan, does not fall into the same period of the solar year.

When I first thought of creating a web-based dynamic calendar, using PHP programming language, I found that there was a Muslim site that had a working code. However, they were unwilling to share their code, and only allowed – as they still do – to link to their calendars.

So I was “forced” to think of a solution, and I designed a Hijri-Gregorian calendar about 5 years ago, when I was building a Muslim portal with several forums. The Muslim portal although is still online, has been closed down. On the right column of the Muslim portal, one can see a functioning Hijri-Gregorian calendar.

Sharing the PHP code:

Well, I have decided to share the code for everyone to be able to adapt to his/her website. I never refused to share the code, and as far as I know nobody asked me in the past to share the code. If you really want to control the calendar and use it properly perhaps you need the code. Anyone may adapt to his/her site and does not know how to, I may help, so here is my formal offer of assistance. The only request I make is not to remove the header of the code which mentions the version of code and other things. The code is named icalendar.

Please Note: I am appealing on behalf  of our Muslim brethren in Myanmar (ex-Burma). Described by the UN as being amongst the most persecuted people in the world, over 80,000 Rohingyan people (Burmeses Muslims) have been left without shelter and protection from the recent violence on them. If you are reading this appeal and want to help, donate your money, whatever you can give, even if a dollar or an euro,  direct to Muslim Aid, an UK based Muslim charity that I trust.

So here is the icalendar code:


<?php
/* Hijri-Gregorian Calendar v.1.0.
by Tayeb Habib of www.redacacia.wordpress.com email tayeb.habib@gmail.com
a special thanks to Khaled Mamdouh of www.vbzoom.com for Hijri Conversion function.
Updated, and added Islamic names of months by Samir Greadly xushi@xushi.homelinux.org.
The code is "ehsalle-sawab" (benefit of the soul) of late Abdul Habib Mohamed, of
Pemba, Mozambique,late father of the author of this code.
*/

//set here font, background etc for the calendar
$fontfamily = isset($fontfamily) ? $fontfamily : "Verdana";
$defaultfontcolor = isset($defaultfontcolor) ? $defaultfontcolor : "#000000";
$defaultbgcolor = isset($defaultbgcolor) ? $defaultbgcolor : "#E0E0E0";
$defaultwbgcolor = isset($defaultwbgcolor) ? $defaultwbgcolor : "#F5F4D3";
$todayfontcolor = isset($todayfontcolor) ? $todayfontcolor : "#000000";
$todaybgcolor = isset($todaybgcolor) ? $todaybgcolor : "#F2BFBF";
$monthcolor = isset($monthcolor) ? $monthcolor : "#000000";
$relfontsize = isset($relfontsize) ? $relfontsize : "1";
$cssfontsize = isset($cssfontsize) ? $cssfontsize : "7pt";

// obtain month, today date etc
$month = (isset($month)) ? $month : date("n",time());
$monthnames = array("January","February","March","April","May","June","July","August","September","October","November","December");
$textmonth = $monthnames[$month - 1];
$year = (isset($year)) ? $year : date("Y",time());
$today = (isset($today))? $today : date("j", time());
$today = ($month == date("n",time())) ? $today : 32;

// The Names of Hijri months
$mname = array("Muharram","Safar","Rabi'ul Awal","Rabi'ul Akhir","Jamadil Awal","Jamadil Akhir","Rajab","Sha'ban","Ramadhan","Shawwal","Zul Qida","Zul Hijja");
// End of the names of Hijri months

// Setting how many days each month has
if ( (($month <8 ) && ($month % 2 == 1)) || (($month > 7) && ($month % 2 ==
0)) ) $days = 31;
if ( (($month <8 ) && ($month % 2 == 0)) || (($month > 7) && ($month % 2 ==
1)) )
$days = 30;

//checking leap year to adjust february days
if ($month == 2)
$days = (date("L",time())) ? 29 : 28;

$dayone = date("w",mktime(1,1,1,$month,1,$year));
$daylast = date("w",mktime(1,1,1,$month,$days,$year));
$middleday = intval(($days-1)/2);

//checking the hijri month on beginning of gregorian calendar
$date_hijri = date("$year-$month-1");
list ($HDays, $HMonths, $HYear) = Hijri($date_hijri);
$smon_hijridone = $mname[$HMonths-1];
$syear_hijridone = $HYear;

//checking the hijri month on end of gregorian calendar
$date_hijri = date("$year-$month-$days");
list ($HDays, $HMonths, $HYear) = Hijri($date_hijri);
$smon_hijridlast = $mname[$HMonths-1];
$syear_hijridlast = $HYear;
//checking the hijri month on middle of gregorian calendar
$date_hijri = date("$year-$month-$middleday");
list ($HDays, $HMonths, $HYear) = Hijri($date_hijri);
$smon_hijridmiddle = $mname[$HMonths-1];
$syear_hijridmiddle = $HYear;

// checking if there's a span of a year
if ($syear_hijridone == $syear_hijridlast) {
$syear_hijridone = "";
}

//checking if span of month is only one or two or three hijri months
if (($smon_hijridone == $smon_hijridmiddle) AND ($smon_hijridmiddle == $smon_hijridlast)) {
$smon_hijri = "<font color=red>".$smon_hijridone." ".$syear_hijridlast."</font>";
}

if (($smon_hijridone == $smon_hijridmiddle) AND ($smon_hijridmiddle != $smon_hijridlast)) {
$smon_hijri = "<font color=red>".$smon_hijridone." ".$syear_hijridone."-".$smon_hijridlast." ".$syear_hijridlast."</font>";
}
if (($smon_hijridone != $smon_hijridmiddle) AND ($smon_hijridmiddle == $smon_hijridlast)) {
$smon_hijri = "<font color=red>".$smon_hijridone." ".$syear_hijridone."-".$smon_hijridlast." ".$syear_hijridlast."</font>";
}

if (($smon_hijridone != $smon_hijridmiddle) AND ($smon_hijridmiddle != $smon_hijridlast)) {
$smon_hijri = "<font color=red>".$smon_hijridone." ".$syear_hijridone."-"."-".$smon_hijridmiddle."-".$smon_hijridlast." ".$syear_hijridlast."</font>";
}
// next part of code generates calendar
?>

<div align="center">
<center><table border="0" cellpadding="0" cellspacing="1" width="100%"
bgcolor='black'>
<tr>
<td valign="top" align="center">
<table border="1" cellpadding="0" cellspacing="0" width="100%"
bgcolor='white'
valign='top'>
<tr>
<td bgcolor="#C6D4E5" colspan="7" align="center"><font color="<?php echo
$monthcolor ?>" face="Verdana" size="2"><b><?PHP echo
$textmonth." ".$year."<br />".$smon_hijri
?></b></font></td>
</tr>
<tr>
<td bgcolor="<?PHP echo $defaultwbgcolor ?>" valign="middle" align="center"
width="15%"><font face="<?PHP echo $fontfamily ?>"
size="1"><b> S </b></font></td>
<td bgcolor="<?PHP echo $defaultwbgcolor ?>" valign="middle" align="center"
width="14%"><font face="<?PHP echo $fontfamily ?>"
size="1"><b> M </b></font></td>
<td bgcolor="<?PHP echo $defaultwbgcolor ?>" valign="middle" align="center"
width="14%"><font face="<?PHP echo $fontfamily ?>"
size="1"><b> T </b></font></td>
<td bgcolor="<?PHP echo $defaultwbgcolor ?>" valign="middle" align="center"
width="14%"><font face="<?PHP echo $fontfamily ?>"
size="1"><b> W </b></font></td>
<td bgcolor="<?PHP echo $defaultwbgcolor ?>" valign="middle" align="center"
width="14%"><font face="<?PHP echo $fontfamily ?>"
size="1"><b> T </b></font></td>
<td bgcolor="<?PHP echo $defaultwbgcolor ?>" valign="middle" align="center"
width="14%"><font face="<?PHP echo $fontfamily ?>"
size="1"><b> F </b></font></td>
<td bgcolor="<?PHP echo $defaultwbgcolor ?>" valign="middle" align="center"
width="15%"><font face="<?PHP echo $fontfamily ?>"
size="1"><b> S </b></font></td>
</tr>
<?

if($dayone != 0)
$span1 = $dayone;
if(6 - $daylast != 0)
$span2 = 6 - $daylast;

for($i = 1; $i <= $days; $i++):
$dayofweek = date("w",mktime(1,1,1,$month,$i,$year));
$width = "14%";

if($dayofweek == 0 || $dayofweek == 6)
$width = "15%";

if($i == $today):
$fontcolor = $todayfontcolor;
$bgcellcolor = $todaybgcolor;
endif;
if($i != $today):
$fontcolor = $defaultfontcolor;
$bgcellcolor = $defaultbgcolor;
endif;
$x = strlen($i);
if ($x == 1){ $b = "0".$i;}
if ($x == 2){ $b = $i;}

$x = strlen($month);
if ($x == 1){ $c = "0".$month;}
if ($x == 2){ $c = $month;}
$data=$year."-".$c."-".$b;

if($i == 1 || $dayofweek == 0):
echo " <tr bgcolor=\"$defaultbgcolor\">\n";
if($span1 > 0 && $i == 1)
echo " <td align=\"left\" bgcolor=\"#999999\"
colspan=\"$span1\"><font face=\"null\" size=\"1\"> </font></td>\n";
endif;
?>
<td bgcolor="<?=$bgcellcolor ?>" valign="middle" align="center"
width="<?=$width ?>">
<?PHP
?><font color="<?PHP echo $fontcolor ?>" face="<?=$fontfamily ?>" size="1"><?

$date_hijri = date("$year-$month-$i");
list ($HDays, $HMonths, $HYear) = Hijri($date_hijri);
if ($HDays == 30) {
$i = $i + 1;
$date_hijri = date("$year-$month-$i");
list ($HDays, $HMonths, $HYear) = Hijri($date_hijri);
if ($HDays == 2) {
$HDays = 1;
}
else {
$HDays = 30;
}
$i = $i - 1;
}

$sday_hijri = $i."<br/><font color=red>".$HDays."</font>";
// display da data
echo $sday_hijri;
?>
</td>
<?PHP
if($i == $days):
if($span2 > 0)
echo " <td align=\"left\" bgcolor=\"#999999\"
colspan=\"$span2\"><font face=\"null\" size=\"1\"> </font></td>\n";
endif;
if($dayofweek == 6 || $i == $days):
echo " </tr>\n";
endif;
endfor;
$ano = str_replace("20", "", $year);

$x = strlen($today);
if ($x == 1){ $b = "0".$today;}
if ($x == 2){ $b = $today;}
//echo $b;
$x = strlen($month);
if ($x == 1){ $c = "0".$month;}
if ($x == 2){ $c = $month;}
//echo $c;

$data=$year.$c.$b;
?>
<?php
// Hijri conversion function
// Copyright 2002 by Khaled Mamdouh www.vbzoom.com. Updated, and added
// Islamic names of months by Samir Greadly xushi @xushi.homelinux.org

function Hijri($GetDate)
{

$TDays=round(strtotime($GetDate)/(60*60*24));
$HYear=round($TDays/354.37419);
$Remain=$TDays-($HYear*354.37419);
$HMonths=round($Remain/29.531182);
$HDays=$Remain-($HMonths*29.531182);
$HYear=$HYear+1389;
$HMonths=$HMonths+10;
$HDays=$HDays+23;

// If the days is over 29, then update month and reset days
if ($HDays>29.531188 and round($HDays)!=30)
{
$HMonths=$HMonths+1;
$HDays=Round($HDays-29.531182);
}

else
{
$HDays=Round($HDays);
}

// If months is over 12, then add a year, and reset months
if($HMonths>12)
{
$HMonths=$HMonths-12;
$HYear=$HYear+1;
}

return array ($HDays, $HMonths, $HYear);
}
// end of Hijri Conversion function
?>
</table>
</td>
</tr>

</td></tr>

</table>

Here’s the icalendar code. You just need to rename the actual .txt extension to .php before using it. You may also need to fit the code into a table, if you know a bit about html coding.

Those who are used to structured programming, may not find this code to be very elegant, or friendly. However, it does work! It has been quite precise for past 5 years, and has been working without failing. As a recent example, the muslim month of Ramadhan  started exactly on the solar date that was indicated in my Hijri-Gregorian calendar.

If you find the code useful or will be appying to your website please donate to our brethren in need in Myanmar. Please check the website I have set up for directly sending money to those in need. Please donate your money, whatever you can give, even if a dollar or an euro,  direct to Muslim Aid, an UK based Muslim charity that I trust.

Have a try with the code! Please do not remove the first part of code with information on version and acknowledgements as I have requested above. You are free to use this code as you please! I will appreciate to know if you are using this code, and on any modifications you, or anyone else, may have made to it. Please notify me here, or through my email tayeb.habib@gmail.com of any improvements or changes you have made.

About these ads

About Tayeb

Electronics engineer, part-time webmaster and owner of "Aliatron", a tech-oriented company registered in Portugal and Mozambique.
This entry was posted in Uncategorized. Bookmark the permalink.

68 Responses to Coding in PHP a Hijri-Gregorian Calendar

  1. nomadone says:

    Asalaamu alaikum brother. We really appreciate you sharing your hard work with all of us. May Allah reward you and have mercy on you and your family.

    I copied and pasted the code into a sidebar for my wordpress site I’m developing but it seems some of the php language is not compatible with the wordpress system. Have you ever used this code within a wordpress site? To be honest I’m not quite sure how to use it.

    I tried adding the first part into my functions.php file in wordpress theme but that didn’t work either.

  2. Tayeb says:

    Wa-alaikum as-salaam dear brother,

    Welcome to the blog. If you run the code as a page you will see the calendar.

    Now surely you can’t just cut and paste into another php page. Perhaps you need to use iframe to include the page and display the calendar.

    I don’t know the coding of wordpress functions.php. Do you write php code, or know how to write php programs?

    Drop me an email if I can somehow help you. My email is tayeb.habib@gmail.com.

    Tayeb

  3. nomadone says:

    Eid Mubarak and shukran, I’ve sent you an email. I’ll have to give that iframe method a try then insha-Allah

  4. Tayeb says:

    Wa-alaikum-as-salaam,

    Welcome to my blog.

    I had been away and I saw your comment just today. It seems you have not copied the code properly. I’ll send you an email on the matter and we will discuss from there.

  5. Afrioni says:

    syukron for information and thanks for ur tutorial, i need it…

  6. Pasar Cairo says:

    Assalamualaikum….

    syukron for the code … it;s work..

  7. Ahmed says:

    Assalamualaikum….

    Thanks for the code – May you be rewarded

  8. zaidmavia says:

    assalamualykum nice job Mashallah but wordpress or any blogsite like wordpress blogspot blogme dnt suppoort php , so can u pls write this code in html

  9. suvapapa says:

    Hi,

    I used your code in Dreamweaver. I copy pasted in a new file. In the dreamweaver Design pane it shows the HTML table with the PHP code. But when i run the php file, i get a blank document. I believe somewhere the code is breaking.

    • Tayeb says:

      Welcome to my blog.

      I suggest you copy and past into notepad and save with php extension and then upload to a server. I have never used Dreamweaver to test php code so I cannot advise you on why the code is not running.

      • suvapapa says:

        i saved it in notepad only. and then copied it over.
        i tested yesterday on my office localhost. tonight i will try on my online apache server and update you.

  10. Azman Ishak says:

    Assalamualaikum Brother,

    Thank you for the code, this is what I’m searching for. I have tested the code which I downloaded it from the download link. I’ve tested it but I got an error which is like this:

    Notice: Undefined variable: span1 …….. in line 166.

    Hope you could help me with this.

    Assalamualaikum,

  11. Azman Ishak says:

    And also on line 127, I’ve assigned the $span1 & $span2 to an empty variable like this:

    if($dayone != 0){
    $span1 = $dayone;
    } else {
    $span1 = ”;
    }
    if(6 – $daylast != 0){
    $span2 = 6 – $daylast;
    } else {
    $span2 = ”;
    }

  12. ara says:

    Would really appreciate if someone can convert this to Java!

  13. B. says:

    Esselamu Alejkum!

    May Allah bless you and your family!

    Thank you for sharing the code!

    • Tayeb says:

      Wa-alaikum-as-salaam,

      Welcome to my blog. Barakallah.

      Done and sharing it to please Allah. Noting more! Knowledge belongs to “l.

      • B. says:

        I have just made some small modifications like making all <? tags to <?php because my environment does not allow <? tags and now everything is OK. It works like I wanted.
        But is it possible without much effort to produce the calendar for all months of this year for example? I would like to show on my site all the months (at least the left months of this year) if it is possible without much effort?! Thank you!
        BarakAllah fik!

    • Tayeb says:

      BarakAllah brother.

  14. Abid Shahzad says:

    Asslam-o-Alikum Brother

    JazakAllah

  15. السلام عليكم ورحمة الله
    I was googling through web to find out some secured php code for a hijri-calendar for my future website: dohliz and I didn’t get it within a week but now!
    I get it from this blog . Thanks to Tayeb for sharing this code. Thanks a lot and جزاك الله خيرا

    • Tayeb says:

      Wa-alaikum-as-salaam. Welcome to this blog.

      Barakallah brother. Do share links when you implement the code.

      When I first wrote the code there was a site that had a working calendar but the author did not want to share the code. He was allowing php include of his calendars. So I had to write the code based on a class that was on an Arab coders’ site, mentioned in my code. It is a good and fair practise to acknowledge authors abd respect intelectual property.

      By sharing a code that on my websites has shown itself to be precise for past 8 years including this Ramadhan, I also am dedicating ehsalle sawab to my late dad Abdul Habib Mohamed of Pemba, Mozambique. My dad decided to educate me and make me an engineer sending me to Brtiain when I was just 16 years old.

  16. Sara says:

    Esselamu alejkum!
    So far using it, I am having a problem with the calendar. This Ramadan was with 30 days, but the calendar displays it with 29 days, and so the dates are all ahead by 1 day.
    I checked the code and as far as I understood it is configured to make one month with 29 days and the other with 30 days,,, but maybe I am wrong in my understanding. Could you help please! My calendar e.g. is showing today 3 September 2012 as 17 Shawwal 1433….
    Thank you in advance!
    Barak Allah fik!

    • Tayeb says:

      Wa-alaikum-as-salaam dear sister in Islam. Welcome to the blog.

      This is a common issue. It happens because the beginning and end of Ramadhan depends on moon’s visibility. This calendar code has been accurate in Portugal, where I reside for past eight years. I cannot guarantee that it will be right next year.

      If you don’t know how to change the date manually please drop me an email (tayeb.habib@gmail.com) and I will send you a modification with a commentary so that in future you can manually change the date. Really it is impossible to design a code that depends on human eyes seing themoon visibility can be too brief, or the moon can be behind clouds. There are lots of indeterminants unfortunately.

  17. thanks for sharing
    jazakallah
    brother i just want to display the hijri date in my website
    can u plz share the code
    thanks

    • Tayeb says:

      Welcome to the blog.

      The code is in the article just read through. If you don’t want to cut and paste the whole code can be download form the link I have provided.

      It is php code and will generate a calendar. You will need to fit into a table and perhaps embed with iframe.

      Any doubts do post here. If successful also post here.

      • thanks for the quick reply
        brother tayeb i dont want to display calender i just want to display current date in hijri
        like this 28 Dhul-Qa’idah 1433 H in one line thats it
        hope u understand my point
        thanks

      • Tayeb says:

        I have done this long time ago. If you check the source of http;//myiwc.com you will find there Javascript code.

      • excellent, brother
        i copied the Javascript code and its works.
        normally in india it is 1 day delay, can u plz guide me how can i delay the date
        jazakallah and thank u very much for your great help

      • Tayeb says:

        As I have said I have done the code long time ago. I copied from somewhere else. I think there is a variable where you can add one day. You may have to do it more often. However our ullama only look for Moon during Ramadhan so it ought not be a problem.

        In this page there is an appeal for Rohingya in Burma. Please help to divulge the link I have created for donations. Nobody seems to have attempted to give any money yet. Money goes straight to a British charity Muslim Aid

    • mohamedirfan says:

      i tried to delay 1 day but not succeeded, currently i use some other script
      thanks again for your great support

      http://jamaateislamihind.in/eng/

      • Tayeb says:

        Surely you did not do the right change. It is something that you will have to do regularly at least once a year when Ramadhan comes when the Ulama look for the moon. Moon cycles are mathematically accurate however our Ulama use watches and other modern devices but are unable to agree upon a point of fitnah among Muslims the viewing of the moon.

  18. Hossamzee says:

    Thank you sir, your code has inspired me alot, thank you again.

  19. Pingback: فئة (كلاس) لعرض تقويم سنة هجرية. | حسام الزغيبي

  20. shahid says:

    Can you tell how can i show full year calender currently just showing current month?

    • Tayeb says:

      Welcome to my blog. One of the commentators has done it. Check the comments and links.

      • shahid says:

        I need with Gregorian calender. Can its possible with your code or not?

      • Tayeb says:

        I do not understand what you want to do. Do you want to create an annual calendar or a monthly calendar?

        My code displays the actual calendar and emphasizes the actual date.

        One of the commentators here shows a link with a new class that displays calendar for a whole year.

        Another one created on basis of our calendar a system that can go backward and forward in months.

        The idea behind sharing the code, is to allow anyone to use this code as it is, or make modifications, Two participants here did modficiations and were kind to share their work. Plrase check what they have done.

      • shahid says:

        I want Annual calendar similar to your one month calender.

      • Tayeb says:

        I just said the calendar is as it is.

        Others have cared to share solutions such as you require. Just read the comments and find solutions there. And if needed try to contact directly these brothers that have done the sharing of their extra work.

  21. equinox says:

    Hi there,

    I will use your calendar conversion code (just the php stuff itself) for my framework and get back to you with a copy asap. The main reason is to provide an i18n section to calculate all kinds of holidays for any calendar/date/holiday-type – the hijri calendar is the last one missing.

    So thank you very much :) Feel free to write me an email if you have any questions.

    Regards

  22. Tony Jose says:

    Hi,thanks for your code. As am new to this Islamic calender can you tell me is it the same as Umm al Qura Calendar of Saudi Arabia.

    • Tayeb says:

      Welcome to my blog. Not really. Umm al Qura as far as I know also has observation of moon. My calendar is solely calculation-based thought it uses the coordinates of Makkah to facilitate a basic standard. It is intended to be purely for indication of approximate dates, though I may say it has been coincidental with Umm al Qura for past years.

  23. dipakkumar5 says:

    sir i am working on a project where i need to display hijri calendar. i have used your code but my client is saying that the current date should be one day less. please it would be a great help to me if you could solve my problem.
    url: http://dwaportfolio.com/projects/bund/veranstaltungskalender/

    • Tayeb says:

      Welcome to my blog. You can cater for extra ou less day by adding a variable which you can change to add to day variable. My Islamic calendar is based on calculation and it has always one or another time this issue specially at beginning and end of Ramadhan when the moon is observed with naked eye.

  24. Ali Hasan says:

    Salam,
    How can the +-1 day can be adjusted? Can you please kindly tell me which variable to adjust? Suppose I have a variable assigned as:
    $adjustDay = “0”;
    and this can be adjusted to = “1” depending on if a day needed to be added to make the Hijri month increase or = “-1″ to decrease or leave as = “0” to keep as is.
    Where would this value be added to? Should this be added to $HDays ? Where should it go? Please help kindly. Thank you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s