WiseOne
05-13-2003, 06:55 AM
I hav posted source code which is in javascript @ http//www.mpadc.com/forum/viewtopic.php?t=262
Lets all php pros get2gather 2 convert the code into php plus add some more features to it, so some of our brothers can use it on there site...
wizard
05-14-2003, 06:03 AM
Assalamu 'alaikum,
I have made a class out of the javascript code you sent. The class works correct and gives exact dates the javasript function gives.
ENJOY!!!
<?php
/**
* Converts Gregorian date to Arabic date and Arabic date to
* Gregorian date
*
*
* greg_to_arabic() will the corresponding Arabic date for a
* Gregorian date and arabic_to_greg() will give the
* corresponding Arabic date for the Gregorian date.
*
* To convert Gregorian date to Arabic date, do this :
* <code>
* $c=new Ara_greg;
* $dr=$c->greg_to_arabic("1928-12-31");
* print $dr;
* </code>
* Remember that the date you sent in should be in
* yyyy-mm-dd format ALWAYS.
*
*
* To convert Arabic date to gregorian date, do this:
* <code>
* $c=new Ara_greg;
* $dr=$c->arabic_to_greg("1402-08-20"); //again, yyyy-mm-dd format
* print $dr
* </code>
*
* A longer example:
*
* <code>
* $a=new Ara_greg;
* print arabic_to_greg("1981-08-01");
* print "<BR>";
* print greg_to_arabic("1424-1-9); //09 or 9 are the same
* </code>
*
* If you optimize this script, please let me know.
*
*ENJOY!!!
*
*
*
* @author Abdullah
* @package Ara_greg
* @version 1.0.0
*/
class Ara_greg {
/**
* Week days
*/
var $week_day = array(
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
);
/**
* Gives the name of the day
*
* @param $wdn The number of the day's name wanted
*/
function get_week_day_name($wdn)
{
return $week_day[$wdn];
}
/**
* Finds the corresponding Arabic date for the Gregorian date given
*
* @param $date The Gregorian date for which the corresponsing Arabic
* date is to be found. The date should be in yyyy-mm-dd format.
*/
function greg_to_arabic($date)
{
list($year,$month,$day)=split("-",$date);
if (($year>1582) || ($year==1582 && $month>10) || (($year==1582) && ($month==10) && ($day>14))) {
$jd=intval((1461*($year+4800+intval(($month-14)/12)))/4) +
intval((367*($month-2-12*(intval(($month-14)/12))))/12)-
intval((3*(intval(($year+4900+intval(($month-14)/12))/100)))/4)+$day-32075;
} else {
$jd = 367*$year-intval((7*($year+5001+intval(($month-9)/7)))/4)+intval((275*$month)/9)+$day+1729777;
}
$l = $jd-1948440+10632;
$n = intval(($l-1)/10631);
$l = $l-10631*$n+354;
$j = (intval((10985-$l)/5316))*(intval((50*$l)/17719))+(intval($l/5670))*(intval((43*$l)/15238));
$l = $l-(intval((30-$j)/15))*(intval((17719*$j)/50))-(intval($j/16))*(intval((15238*$j)/43))+29;
$month = intval((24*$l)/709);
$day = $l-intval((709*$month)/24);
$year = 30*$n+$j-30;
return $this->format_date("$year-$month-$day");
}
/**
* This function will return the date in the requested format
*
* For now, this function has not been programmed. But, in the next version of this script,
* users will be able to tell the functions to return the dates in a specific format. For now,
* I will keep it a private function
*
* @access private
* @param $format The format to return the date in
* @param $date The date that needs to be formated
*/
function format_date($date,$format="")
{
list($year,$month,$year) = split("-",$date);
return $date;
}
function arabic_to_greg($date,$return_format="")
{
list($year,$month,$day) = split("-",$date);
$jd = intval((11*$year+3)/30)+354*$year+30*$month-intval(($month-1)/2)+$day+1948440-385;
//again, weekday will be $jd/7
if ($jd>2299160) {
$l = $jd+68569;
$n = intval((4*$l)/146097);
$l = $l-intval((146097*$n+3)/4);
$i = intval((4000*($l+1))/1461001);
$l = $l-intval((1461*$i)/4)+31;
$j = intval((80*$l)/2447);
$day=$l-intval((2447*$j)/80);
$l = intval($j/11);
$month = $j+2-12*$l;
$year = 100*($n-49)+$i+$l;
} else {
$j=$jd+1402;
$k=intval(($j-1)/1461);
$l=$j-1461*$k;
$n=intval(($l-1)/365)-intval($l/1461);
$i=$l-365*$n+30;
$j=intval((80*$i)/2447);
$day=$i-intval((2447*$j)/80);
$i=intval($j/11);
$month=$j+2-12*$i;
$year=4*$k+$n+$i-4716;
}
$date="$year-$month-$day";
return $this->format_date($date,$return_format);
}
}
?>
Powered by vBulletin™ Version 4.0.1 Copyright © 2010 vBulletin Solutions, Inc. All rights reserved.