
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:
<?php
$serendipity['smarty']->register_function('hdate', 'hdate');
function hdate($params, &$smarty)
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.