Adding Hijri date to serendipity entries

The other day I came across this website that was promoting a wordpress plugin that displays the hijri date in blog entries. I thought this was a fantastic idea and I should try and implement this on my site. As a Muslim I think I ought use the hijri calendar system. I also thought it would be easy to implement this given the wide choice of open source software available on the net. I didn’t think I could program the actual gregorian to hijri conversion myself. I just wanted a php script that I could modify. Unfortunately the code provided on that site was not upto the mark. It kept outputting incorrect dates. And despite my initial optimism it took lots and lots of googling before I came across a useable script. In this post I describe how I modified this script and integrated it with serendipity to achieve the effect you see above (at least at the time of writing). I also provide links to the neccessary code so you can implement this same effect on your blog!

I found a script written by Salah Faya on the php4arab site.  Although though the original script outputs accurate dates it did not suit my purposes so I modified it. I changed the encoding of arabic charactes to the default Serendipity UTF-8 encoding. This means that this script can be used to add Arabic hijri dates to mixed language pages such as mine. Since I did not have access to an arabic word processor I used wikipedia :). I also added the option to display the hijri month and day names in english. For ex: 05 Jamadi-ul-Awwal 1427 AH. The new script also displays numbers in arabic script.

This script contains a class ArabicTools. It contains a public method arabicDate that accepts two arguments – a format string and unix timestamp. The format string has two sections an option or type section and a format section:

ah: d f Y

In the above example everything before the colon is the type of date i.e. “ah” is the type. “d f y” is the date format. The two types of date that arabicDate can output  are:

  • ah – Hijri date with month and day names in English
  • hj – Hijri date with month and day names in Arabic
  • Adding a * infront of the type options like “*hj” causes it to
    encase the date within a span with dir=rtl so that the browser displays
    arabic script in right to left fashion.

Some common date format options include:

  • d for two digit date
  • f for name of  month
  • l for name of day
  • Y for four digit year

arabicDate returns a string containing the hijri date. To add the dates to s9y I created a config.inc.php file in my template’s directory. Usually this directory is [serendipty_root]/templates/[template_name]. In this file I can add functions that can be accessed via smarty tags in the .tpl files (such as entries.tpl) . I added the following function:

register_function('hdate', 'hdate');

function hdate($params, &$smarty)
{ global $serendipity;
include_once("arabictools.class.php");
$tistamp=$params['tstamp']+($serendipity['serverOffsetHours']*3600);
return ArabicTools::arabicDate($params['format'],$tistamp);
}

?>

This function starts off by registering the hdate function with smarty so it can be accessed via the .tpl files. Function hdate accepts two arguments from smarty tags. These parameters are stored in the $params array. They are the entry timesttamp and date format. function hdate starts off by including the arabictools file which is placed in the same directory as config.inc.php i.e. in the template directory. It then adds the server time offset that you might have specified in serendipity configuration options to the entry time stamp and returns the hijri date. You can access this function from the entries.tpl file with the following code:

<div id="hdate"> {hdate tstamp=$entry.timestamp format='*hj:d F Y'}
</div>

As you can see I have added a div around the tag to allow you to style it with css. The function call itself is simple enough. You insert the above code exactly where you want to display the date in your .tpl file. In my case I have inserted it inside the serendipity_date div. You can customise the date format as well. You can download zipped up modified ArabicTools file, the config.inc.php file and a sample entries.tpl file (for the default s9y template) here.

If you are using s9y 0.9 or earlier you might have problems previewing entries after making these changes. This is because of a bug in s9y which you can correct by reading this thread in the s9y forums.

15 thoughts on “Adding Hijri date to serendipity entries

  1. Peace.
    We’ve been rather busy here at YSE which explains the lack of any news for a while, we are very dissatisfied with the hosting company for refusing to enable FreeType support for the HijriDate wizard which was due for release back in April …

  2. Dear blogger,

    I am the developer of Persian calendar for Serendipity.
    Are you agree to develop Arabic calendar built-in in Serendipity?
    if “yes”, please send an email to me.

    regards,
    Omid

  3. Salam,
    Thank you for your interesting and important subject, but what made me really impressed is how you are showing the Hijri date in the Arabic Numbers(Hindi Numerial), because this is really un-usual specially when you go to any arabic site and they are showing numbers in English or Latin Numbers.
    So please Can you help me with that and tell me how you did that.

    Best regards,

    Salam

    • I just included the appropriate unicode characters in the script. I copied them from the wikipedia articles:

      http://en.wikipedia.org/wiki/Eastern_Arabic_numerals
      http://en.wikipedia.org/wiki/Ottoman_Turkish_script

      • Thank you very much for this hint, it is really simple but cleaver, I never thought of it this way, I was always looking for a solution in css or changing the charset of the page but none worked.
        Thank you again
        Best regards

        Salam

        • Hello Dear i also want to use Hijri Date and time in arabic at my webpage and i did not uderstand any method or dont have any script kindly u help me or plz send me script that u understood From blogger .i just want to use simple HTML code not php or any other language so plz plz plz help me waiting for ur response

  4. I recently discovered the "markup: smarty" event plugin in spartacus! It allows you to add smarty tags in your entries and sidebar html nuggets. I installed it and am using smarty tags within an HTML nugget sidebar plugin to selectively display

  5. Salmu Alaikum,
    Is there a way to add this feature (Hijri date) to a regular html page (not a php). Please advise.
    Thank you

    • Yes you have a few options.

      You can use just html to insert an image with the current hijri date using this tool:

      http://www.yse-uk.com/hijridate/index.php

      There are also some sites offering javascript widgets:

      http://al-habib.tripod.com/hijricalendar/

      Just search the web for “hijri date” to find more options.

      • Thank you for your response. I like the first one but unfortunately it outputs latin characters and I was hoping for it to have arabic characters. I had hard time finding an easy and straight-forward code to implement the hijri date in arabic.

  6. hi ! with this class show number english ! how to show number to arabic ? example : your blog
    ١٩ شعبان ١٤٢٩ . note : i show this date in index page . thanks ! regards

    • Call arabicDate wit the format string:

      hj: d f Y

      hj=arabic numerals and month names arabic script. Please see the blog post above for more details.

Leave a Reply

Your email address will not be published. Required fields are marked *