<?php
/*
Jalali Date function by Milad Rastian (miladmovie AT yahoo DOT com)

//The main function which convert Gregorian to Jalali calendars is:
// Copyright (C) 2000  Roozbeh Pournader and Mohammad Toossi 
//you can see complete note of those function in down of the page

		AND JALAI DATE FUNCTION
this function is simillar than date function in PHP
you can find more about it in http://jdf.farsiprojects.com
		Copyright (C)2003 FARSI PROJECTS GROUP
*/

function jdate($type,$maket="now")
{
	//Sets local timezone to Asia/Tehran to solve Default TimeZone problem
	putenv('TZ=Asia/Tehran');
	//set 1 if you want translate number to farsi or if you don't like set 0
	$transnumber=0;
	///chosse your timezone
	$TZhours=0;
	$TZminute=0;
	$need="";
	$result1="";
	$result="";
	if($maket=="now"){
		$year=date("Y");
		$month=date("m");
		$day=date("d");
		list( $jyear, $jmonth, $jday ) = gregorian_to_jalali($year, $month, $day);
		$maket=mktime(date("H")+$TZhours,date("i")+$TZminute,date("s"),date("m"),date("d"),date("Y"));
	}else{
		//$maket=0;
		$maket+=$TZhours*3600+$TZminute*60;
		$date=date("Y-m-d",$maket);
		list( $year, $month, $day ) = preg_split ( '/-/', $date );

		list( $jyear, $jmonth, $jday ) = gregorian_to_jalali($year, $month, $day);
		}

	$need= $maket;
	$year=date("Y",$need);
	$month=date("m",$need);
	$day=date("d",$need);
	$i=0;
	$subtype="";
	$subtypetemp="";
	list( $jyear, $jmonth, $jday ) = gregorian_to_jalali($year, $month, $day);
	while($i<strlen($type))
	{
		$subtype=substr($type,$i,1);
		if($subtypetemp=="\\")
		{
			$result.=$subtype;
			$i++;
			continue;
		}
		
		switch ($subtype)
		{

			case "A":
				$result1=date("a",$need);
				if($result1=="pm") $result.= "&#1576;&#1593;&#1583;&#1575;&#1586;&#1592;&#1607;&#1585;";
				else $result.="&#1602;&#1576;&#1604;&#8207;&#1575;&#1586;&#1592;&#1607;&#1585;";
				break;

			case "a":
				$result1=date("a",$need);
				if($result1=="pm") $result.= "&#1576;&#46;&#1592;";
				else $result.="&#1602;&#46;&#1592;";
				break;
			case "d":
				if($jday<10)$result1="0".$jday;
				else 	$result1=$jday;
				if($transnumber==1) $result.=Convertnumber2farsi($result1);
				else $result.=$result1;
				break;
			case "D":
				$result1=date("D",$need);
				if($result1=="Thu") $result1="&#1662;";
				else if($result1=="Sat") $result1="&#1588;";
				else if($result1=="Sun") $result1="&#1609;";
				else if($result1=="Mon") $result1="&#1583;";
				else if($result1=="Tue") $result1="&#1587;";
				else if($result1=="Wed") $result1="&#1670;";
				else if($result1=="Thu") $result1="&#1662;";
				else if($result1=="Fri") $result1="&#1580;";
				$result.=$result1;
				break;
			case"F":
				$result.=monthname($jmonth);
				break;
			case "g":
				$result1=date("g",$need);
				if($transnumber==1) $result.=Convertnumber2farsi($result1);
				else $result.=$result1;
				break;
			case "G":
				$result1=date("G",$need);
				if($transnumber==1) $result.=Convertnumber2farsi($result1);
				else $result.=$result1;
				break;
				case "h":
				$result1=date("h",$need);
				if($transnumber==1) $result.=Convertnumber2farsi($result1);
				else $result.=$result1;
				break;
			case "H":
				$result1=date("H",$need);
				if($transnumber==1) $result.=Convertnumber2farsi($result1);
				else $result.=$result1;
				break;
			case "i":
				$result1=date("i",$need);
				if($transnumber==1) $result.=Convertnumber2farsi($result1);
				else $result.=$result1;
				break;
			case "j":
				$result1=$jday;
				if($transnumber==1) $result.=Convertnumber2farsi($result1);
				else $result.=$result1;
				break;
			case "l":
				$result1=date("l",$need);
				if($result1=="Saturday") $result1="&#1588;&#1606;&#1576;&#1607;";
				else if($result1=="Sunday") $result1="&#1610;&#1603;&#1588;&#1606;&#1576;&#1607;";
				else if($result1=="Monday") $result1="&#1583;&#1608;&#1588;&#1606;&#1576;&#1607;";
				else if($result1=="Tuesday") $result1="&#1587;&#1607;&#32;&#1588;&#1606;&#1576;&#1607;";
				else if($result1=="Wednesday") $result1="&#1670;&#1607;&#1575;&#1585;&#1588;&#1606;&#1576;&#1607;";
				else if($result1=="Thursday") $result1="&#1662;&#1606;&#1580;&#1588;&#1606;&#1576;&#1607;";
				else if($result1=="Friday") $result1="&#1580;&#1605;&#1593;&#1607;";
				$result.=$result1;
				break;
			case "m":
				if($jmonth<10) $result1="0".$jmonth;
				else	$result1=$jmonth;
				if($transnumber==1) $result.=Convertnumber2farsi($result1);
				else $result.=$result1;
				break;
			case "M":
				$result.=short_monthname($jmonth);
				break;
			case "n":
				$result1=$jmonth;
				if($transnumber==1) $result.=Convertnumber2farsi($result1);
				else $result.=$result1;
				break;
			case "s":
				$result1=date("s",$need);
				if($transnumber==1) $result.=Convertnumber2farsi($result1);
				else $result.=$result1;
				break;
			case "S":
				$result.="&#1575;&#1605;";
				break;
			case "t":
				$result.=lastday ($month,$day,$year);
				break;
			case "w":
				$result1=date("w",$need);
				if($transnumber==1) $result.=Convertnumber2farsi($result1);
				else $result.=$result1;
				break;
			case "y":
				$result1=substr($jyear,2,4);
				if($transnumber==1) $result.=Convertnumber2farsi($result1);
				else $result.=$result1;
				break;
			case "Y":
				$result1=$jyear;
				if($transnumber==1) $result.=Convertnumber2farsi($result1);
				else $result.=$result1;
				break;		
			case "U" :
				$result.=mktime();
				break;
			case "Z" :
				$result.=days_of_year($jmonth,$jday,$jyear);
				break;
			case "L" :
				list( $tmp_year, $tmp_month, $tmp_day ) = jalali_to_gregorian(1384, 12, 1);
				echo $tmp_day;
				/*if(lastday($tmp_month,$tmp_day,$tmp_year)=="31")
					$result.="1";
				else 
					$result.="0";
					*/
				break;
			default:
				$result.=$subtype;
		}
		$subtypetemp=substr($type,$i,1);
	$i++;
	}
	return $result;
}



function jmaketime($hour="",$minute="",$second="",$jmonth="",$jday="",$jyear="")
{
	if(!$hour && !$minute && !$second && !$jmonth && !$jmonth && !$jday && !$jyear)
		return mktime();
	list( $year, $month, $day ) = jalali_to_gregorian($jyear, $jmonth, $jday);
	$i=mktime($hour,$minute,$second,$month,$day,$year);	
	return $i;
}


///Find num of Day Begining Of Month ( 0 for Sat & 6 for Sun)
function mstart($month,$day,$year)
{
	list( $jyear, $jmonth, $jday ) = gregorian_to_jalali($year, $month, $day);
	list( $year, $month, $day ) = jalali_to_gregorian($jyear, $jmonth, "1");
	$timestamp=mktime(0,0,0,$month,$day,$year);
	return date("w",$timestamp);
}

//Find Number Of Days In This Month
function lastday ($month,$day,$year)
{
	$jday2="";
	$jdate2 ="";
	$lastdayen=date("d",mktime(0,0,0,$month+1,0,$year));
	list( $jyear, $jmonth, $jday ) = gregorian_to_jalali($year, $month, $day);
	$lastdatep=$jday;
	$jday=$jday2;
	while($jday2!="1")
	{
		if($day<$lastdayen)
		{
			$day++;
			list( $jyear, $jmonth, $jday2 ) = gregorian_to_jalali($year, $month, $day);
			if($jdate2=="1") break;
			if($jdate2!="1") $lastdatep++;
		}
		else
		{ 
			$day=0;
			$month++;
			if($month==13) 
			{
					$month="1";
					$year++;
			}
		}

	}
	return $lastdatep-1;
}

//Find days in this year untile now 
function days_of_year($jmonth,$jday,$jyear)
{
	$year="";
	$month="";
	$year="";
	$result="";
	if($jmonth=="01")
		return $jday;
	for ($i=1;$i<$jmonth || $i==12;$i++)
	{
		list( $year, $month, $day ) = jalali_to_gregorian($jyear, $i, "1");
		$result+=lastday($month,$day,$year);
	}
	return $result+$jday;
}


//Find days in this year untile now 
function g_days_of_year($month,$day,$year)
{
	$result="";
	if($month=="01")
		return $day;
	for ($i=1;$i<$month || $i==12;$i++)
	{
		$result+=lastday($month,$day,$year);
	}
	return $result+$day;
}


//translate number of month to name of month
function monthname($month)
{

    if($month=="01") return "&#1601;&#1585;&#1608;&#1585;&#1583;&#1610;&#1606;";

    if($month=="02") return "&#1575;&#1585;&#1583;&#1610;&#1576;&#1607;&#1588;&#1578;";

    if($month=="03") return "&#1582;&#1585;&#1583;&#1575;&#1583;";

    if($month=="04") return  "&#1578;&#1610;&#1585;";

    if($month=="05") return "&#1605;&#1585;&#1583;&#1575;&#1583;";

    if($month=="06") return "&#1588;&#1607;&#1585;&#1610;&#1608;&#1585;";

    if($month=="07") return "&#1605;&#1607;&#1585;";

    if($month=="08") return "&#1570;&#1576;&#1575;&#1606;";

    if($month=="09") return "&#1570;&#1584;&#1585;";

    if($month=="10") return "&#1583;&#1610;";

    if($month=="11") return "&#1576;&#1607;&#1605;&#1606;";

    if($month=="12") return "&#1575;&#1587;&#1601;&#1606;&#1583;";
}

function short_monthname($month)
{

    if($month=="01") return "&#1601;&#1585;&#1608;";

    if($month=="02") return "&#1575;&#1585;&#1583;";

    if($month=="03") return "&#1582;&#1585;&#1583;";

    if($month=="04") return  "&#1578;&#1610;&#1585;";

    if($month=="05") return "&#1605;&#1585;&#1583;";

    if($month=="06") return "&#1588;&#1607;&#1585;";

    if($month=="07") return "&#1605;&#1607;&#1585;";

    if($month=="08") return "&#1570;&#1576;&#1575;";

    if($month=="09") return "&#1570;&#1584;&#1585;";

    if($month=="10") return "&#1583;&#1610;";

    if($month=="11") return "&#1576;&#1607;&#1605;";

    if($month=="12") return "&#1575;&#1587;&#1601; ";
}

////here convert to  number in persian
function Convertnumber2farsi($srting) 
{
	$num0="&#1776;";
	$num1="&#1777;";
	$num2="&#1778;";
	$num3="&#1779;";
	$num4="&#1780;";
	$num5="&#1781;";
	$num6="&#1782;";
	$num7="&#1783;";
	$num8="&#1784;";
	$num9="&#1785;";
	
	$stringtemp="";
	$len=strlen($srting);
	for($sub=0;$sub<$len;$sub++)
	{
	 if(substr($srting,$sub,1)=="0")$stringtemp.=$num0;
	 elseif(substr($srting,$sub,1)=="1")$stringtemp.=$num1;
	 elseif(substr($srting,$sub,1)=="2")$stringtemp.=$num2;
	 elseif(substr($srting,$sub,1)=="3")$stringtemp.=$num3;
	 elseif(substr($srting,$sub,1)=="4")$stringtemp.=$num4;
	 elseif(substr($srting,$sub,1)=="5")$stringtemp.=$num5;
	 elseif(substr($srting,$sub,1)=="6")$stringtemp.=$num6;
	 elseif(substr($srting,$sub,1)=="7")$stringtemp.=$num7;
	 elseif(substr($srting,$sub,1)=="8")$stringtemp.=$num8;
	 elseif(substr($srting,$sub,1)=="9")$stringtemp.=$num9;
	 elseif(substr($srting,$sub,1)==".")$stringtemp.=',';
	 else $stringtemp.=substr($srting,$sub,1);

	}
return   $stringtemp;

}///end conver to number in persian


////here convert to  number in persian
function Convertnumber2farsi2($srting) 
{
	$num0="۰";
	$num1="۱";
	$num2="۲";
	$num3="۳";
	$num4="۴";
	$num5="۵";
	$num6="۶";
	$num7="۷";
	$num8="۸";
	$num9="۹";
	
	$stringtemp="";
	$len=strlen($srting);
	for($sub=0;$sub<$len;$sub++)
	{
	 if(substr($srting,$sub,1)=="0")$stringtemp.=$num0;
	 elseif(substr($srting,$sub,1)=="1")$stringtemp.=$num1;
	 elseif(substr($srting,$sub,1)=="2")$stringtemp.=$num2;
	 elseif(substr($srting,$sub,1)=="3")$stringtemp.=$num3;
	 elseif(substr($srting,$sub,1)=="4")$stringtemp.=$num4;
	 elseif(substr($srting,$sub,1)=="5")$stringtemp.=$num5;
	 elseif(substr($srting,$sub,1)=="6")$stringtemp.=$num6;
	 elseif(substr($srting,$sub,1)=="7")$stringtemp.=$num7;
	 elseif(substr($srting,$sub,1)=="8")$stringtemp.=$num8;
	 elseif(substr($srting,$sub,1)=="9")$stringtemp.=$num9;
	 elseif(substr($srting,$sub,1)==".")$stringtemp.=',';
	 else $stringtemp.=substr($srting,$sub,1);

	}
return   $stringtemp;

}///end conver to number in persian


////here convert to  number in persian
function Convertnumber2latin($srting) 
{
	$num0="0";
	$num1="1";
	$num2="2";
	$num3="3";
	$num4="4";
	$num5="5";
	$num6="6";
	$num7="7";
	$num8="8";
	$num9="9";
	
	$stringtemp="";
	$len=strlen($srting);
	for($sub=0;$sub<$len;$sub++)
	{
	 if(substr($srting,$sub,1)=="&#1776;")$stringtemp.=$num0;
	 elseif(substr($srting,$sub,1)=="&#1777;")$stringtemp.=$num1;
	 elseif(substr($srting,$sub,1)=="&#1778;")$stringtemp.=$num2;
	 elseif(substr($srting,$sub,1)=="&#1779;")$stringtemp.=$num3;
	 elseif(substr($srting,$sub,1)=="&#1780;")$stringtemp.=$num4;
	 elseif(substr($srting,$sub,1)=="&#1781;")$stringtemp.=$num5;
	 elseif(substr($srting,$sub,1)=="&#1782;")$stringtemp.=$num6;
	 elseif(substr($srting,$sub,1)=="&#1783;")$stringtemp.=$num7;
	 elseif(substr($srting,$sub,1)=="&#1784;")$stringtemp.=$num8;
	 elseif(substr($srting,$sub,1)=="&#1785;")$stringtemp.=$num9;
	 else $stringtemp.=substr($srting,$sub,1);

	}
return   $stringtemp;

}///end conver to number in persian


function is_kabise($year)
{
	if($year%4==0 && $year%100!=0)
		return true;
	return false;
}



function jtime()
{
	return mktime()	;
}

function jgetdate($timestamp="")
{
	if($timestamp=="")
		$timestamp=mktime();

	return array(
		0=>$timestamp,	
		"seconds"=>jdate("s",$timestamp),
		"minutes"=>jdate("i",$timestamp),
		"hours"=>jdate("G",$timestamp),
		"mday"=>jdate("j",$timestamp),
		"wday"=>jdate("w",$timestamp),
		"mon"=>jdate("n",$timestamp),
		"year"=>jdate("Y",$timestamp),
		"yday"=>days_of_year(jdate("m",$timestamp),jdate("d",$timestamp),jdate("Y",$timestamp)),
		"weekday"=>jdate("l",$timestamp),		
		"month"=>jdate("F",$timestamp),
	);
}






/*
   $Id: founction/farsi.php, v1.0 2004/08/11 15:37:10 Farhad zare Exp $
   
   FarsiCommerce, Open Source E-Commerce
   http://www.persianoc.com/
   
   Copyright (c) 2005 Farsicommerce
   
   Released under the GNU General Public License
*/
//Jalali convertor functions writer : Roozbeh Pournader and Mohammad Toossi.(Tank you)

//date functions
function div($a,$b) {
    return (int) ($a / $b);
}

$hejri_error = "پارامترهاي تابع تاريخ شمسي نادرست مي باشد.";
$j_month_name = array("","فروردين","ارديبهشت","خرداد","تير","مرداد","شهريور","مهر","آبان","آذر","دي","بهمن","اسفند");
$j_week_name = array("","شنبه","يكشنبه","دوشنبه","سه شنبه","چهارشنبه","پنجشنبه","جمعه");
$g_month_name = array("", "January", "February", "March", "April","May", "June", "July", "August", "September","October", "November", "December");
$g_week_name = array("", "Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday", "Sunday");

$g_days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
$j_days_in_month = array(31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29);

function gregorian_to_jalali ($g_y, $g_m, $g_d)
{
    global $g_days_in_month;
    global $j_days_in_month;

   $gy = $g_y-1600;
   $gm = $g_m-1;
   $gd = $g_d-1;

   $g_day_no = 365*$gy+div($gy+3,4)-div($gy+99,100)+div($gy+399,400);

   for ($i=0; $i < $gm; ++$i)
      $g_day_no += $g_days_in_month[$i];
   if ($gm>1 && (($gy%4==0 && $gy%100!=0) || ($gy%400==0)))
      /* leap and after Feb */
      $g_day_no++;
   $g_day_no += $gd;

   $j_day_no = $g_day_no-79;

   $j_np = div($j_day_no, 12053); /* 12053 = 365*33 + 32/4 */
   $j_day_no = $j_day_no % 12053;

   $jy = 979+33*$j_np+4*div($j_day_no,1461); /* 1461 = 365*4 + 4/4 */

   $j_day_no %= 1461;

   if ($j_day_no >= 366) {
      $jy += div($j_day_no-1, 365);
      $j_day_no = ($j_day_no-1)%365;
   }

   for ($i = 0; $i < 11 && $j_day_no >= $j_days_in_month[$i]; ++$i)
      $j_day_no -= $j_days_in_month[$i];
   $jm = $i+1;
   $jd = $j_day_no+1;

   return array($jy, $jm, $jd);
}

function jalali_to_gregorian($j_y, $j_m, $j_d)
{
    global $g_days_in_month;
    global $j_days_in_month;

   $jy = $j_y-979;
   $jm = $j_m-1;
   $jd = $j_d-1;

   $j_day_no = 365*$jy + div($jy, 33)*8 + div($jy%33+3, 4);
   for ($i=0; $i < $jm; ++$i)
      $j_day_no += $j_days_in_month[$i];

   $j_day_no += $jd;

   $g_day_no = $j_day_no+79;

   $gy = 1600 + 400*div($g_day_no, 146097); /* 146097 = 365*400 + 400/4 - 400/100 + 400/400 */
   $g_day_no = $g_day_no % 146097;

   $leap = true;
   if ($g_day_no >= 36525) /* 36525 = 365*100 + 100/4 */
   {
      $g_day_no--;
      $gy += 100*div($g_day_no,  36524); /* 36524 = 365*100 + 100/4 - 100/100 */
      $g_day_no = $g_day_no % 36524;

      if ($g_day_no >= 365)
         $g_day_no++;
      else
         $leap = false;
   }

   $gy += 4*div($g_day_no, 1461); /* 1461 = 365*4 + 4/4 */
   $g_day_no %= 1461;

   if ($g_day_no >= 366) {
      $leap = false;

      $g_day_no--;
      $gy += div($g_day_no, 365);
      $g_day_no = $g_day_no % 365;
   }

   for ($i = 0; $g_day_no >= $g_days_in_month[$i] + ($i == 1 && $leap); $i++)
      $g_day_no -= $g_days_in_month[$i] + ($i == 1 && $leap);
   $gm = $i+1;
   $gd = $g_day_no+1;

   return array($gy, $gm, $gd);
}

function gregorian_week_day($g_y, $g_m, $g_d)
{
   global $g_days_in_month;

   $gy = $g_y-1600;
   $gm = $g_m-1;
   $gd = $g_d-1;

   $g_day_no = 365*$gy+div($gy+3,4)-div($gy+99,100)+div($gy+399,400);

   for ($i=0; $i < $gm; ++$i)
      $g_day_no += $g_days_in_month[$i];
   if ($gm>1 && (($gy%4==0 && $gy%100!=0) || ($gy%400==0)))
      /* leap and after Feb */
      ++$g_day_no;
   $g_day_no += $gd;

   return ($g_day_no + 5) % 7 + 1;
}

function jalali_week_day($j_y, $j_m, $j_d)
{
   global $j_days_in_month;

   $jy = $j_y-979;
   $jm = $j_m-1;
   $jd = $j_d-1;

   $j_day_no = 365*$jy + div($jy, 33)*8 + div($jy%33+3, 4);

   for ($i=0; $i < $jm; ++$i)
      $j_day_no += $j_days_in_month[$i];

   $j_day_no += $jd;

   return ($j_day_no + 2) % 7 + 1;
}

function jcheckdate($j_m, $j_d, $j_y)
{
   global $j_days_in_month;

   if ($j_y < 0 || $j_y > 32767 || $j_m < 1 || $j_m > 12 || $j_d < 1 || $j_d >
           ($j_days_in_month[$j_m-1] + ($j_m == 12 && !(($j_y-979)%33%4))))
       return false;
   return true;
}

//easy convertor function for hejri date

//base convertor
function tep_formathejri($date) {
        global $j_month_name,$j_week_name ,$hejri_error;
        if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $datetime)) {
                $hejriDate = gregorian_to_jalali($datetime[1],$datetime[2],$datetime[3]);
                $tep_hd = jalali_week_day($hejriDate[0],$hejriDate[1],$hejriDate[2]);
                $datetime = $j_week_name[$tep_hd]."، ".$hejriDate[2]." ".$j_month_name[$hejriDate[1]]."، ".$hejriDate[0];
                return $datetime;
        } else {
                return $hejri_error;
        }
}

function tep_hejri1($date){
        global $hejri_error;
        if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $datetime)) {
                $hejriDate = gregorian_to_jalali($datetime[1],$datetime[2],$datetime[3]);
                $datetime = $hejriDate[2]."/".$hejriDate[1]."/".$hejriDate[0];
                return $datetime;
        } else {
                return $hejri_error;
        }
}

function tep_hejri11($date){
        global $hejri_error;
        if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $datetime)) {
                $hejriDate = gregorian_to_jalali($datetime[1],$datetime[2],$datetime[3]);
                $datetime = $hejriDate[0]."/".$hejriDate[1]."/".$hejriDate[2];
                return $datetime;
        } else {
                return $hejri_error;
        }
}

function tep_hejri2($date){
        global $j_month_name, $hejri_error;
        if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $datetime)) {
                $hejriDate = gregorian_to_jalali($datetime[1],$datetime[2],$datetime[3]);
                $datetime = $hejriDate[2]." ".$j_month_name[$hejriDate[1]]."، ".$hejriDate[0];
                return $datetime;
        } else {
                return $hejri_error;
        }
}

function tep_hejri3($date){
        global $hejri_error;
        if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})", $date, $datetime)) {
                $hejriDate = gregorian_to_jalali($datetime[1],$datetime[2],$datetime[3]);
                $datetime = $hejriDate[0]."/".$hejriDate[1]."/".$hejriDate[2]." ".$datetime[4].":".$datetime[5].":".$datetime[6];
                return $datetime;
        } else {
                return $hejri_error;
        }
}

function tep_hejri30($date){
        global $hejri_error;
        if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})", $date, $datetime)) {
                $hejriDate = gregorian_to_jalali($datetime[1],$datetime[2],$datetime[3]);
                $datetime = $hejriDate[0]."/".$hejriDate[1]."/".$hejriDate[2]." ".$datetime[4].":".$datetime[5];
                return $datetime;
        } else {
                return $hejri_error;
        }
}

function tep_hejri4($date){
        global $j_month_name,$j_week_name, $hejri_error;
        if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})", $date, $datetime)) {
                $hejriDate = gregorian_to_jalali($datetime[1],$datetime[2],$datetime[3]);
                $tep_hd = jalali_week_day($hejriDate[0],$hejriDate[1],$hejriDate[2]);
                $datetime = $j_week_name[$tep_hd]."، ".$hejriDate[2]." ".$j_month_name[$hejriDate[1]];
                return $datetime;
        } else {
                return $hejri_error;
        }
}

function tep_hejri5($date){
        global $hejri_error;
        if (ereg ("([0-9]{1,2})-([0-9]{1,2})-([0-9]{4})", $date, $datetime)) {
                $hejriDate = gregorian_to_jalali($datetime[3],$datetime[2],$datetime[1]);
                $datetime = $hejriDate[2]."-".$hejriDate[1]."-".$hejriDate[0];
                return $datetime;
        } else {
                return $hejri_error;
        }
}

function tep_hejri6($date){
        global $j_month_name,$j_week_name, $hejri_error;
        if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})", $date, $datetime)) {
                $hejriDate = gregorian_to_jalali($datetime[1],$datetime[2],$datetime[3]);
                $datetime = $hejriDate[2]." ".$j_month_name[$hejriDate[1]]."، ".$hejriDate[0]." - ".$datetime[4].":".$datetime[5].":".$datetime[6];
                return $datetime;
        } else {
                return $hejri_error;
        }
}



// now date to hejri
function tep_nowhejri(){
        $now = getdate();
        $now = $now['year']."-".$now['mon']."-".$now['mday']." ".$now['hours'].":".$now['minutes'].":".$now['seconds'];
        $now = tep_formathejri($now);
return $now;
}

// long hejri date
function tep_hejri7($date){
        global $j_month_name,$j_week_name, $hejri_error;
        if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})", $date, $datetime)) {
                $hejriDate = gregorian_to_jalali($datetime[1],$datetime[2],$datetime[3]);
                $tep_hd = jalali_week_day($hejriDate[0],$hejriDate[1],$hejriDate[2]);
                $datetime = $j_week_name[$tep_hd]."، ".$hejriDate[2]." ".$j_month_name[$hejriDate[1]]."، ".$hejriDate[0]." ".$datetime[4].":".$datetime[5].":".$datetime[6];
                return $datetime;
        } else {
                return $hejri_error;
        }
}
function tep_hejri8($date){
        global $j_month_name,$j_week_name, $hejri_error;
        if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})", $date, $datetime)) {
                $hejriDate = gregorian_to_jalali($datetime[1],$datetime[2],$datetime[3]);
                $datetime = $hejriDate[2]." ".$j_month_name[$hejriDate[1]]."، ".$hejriDate[0]." ساعت: ".$datetime[4].":".$datetime[5];
                return $datetime;
        } else {
                return $hejri_error;
        }
}

function tep_hejri9($date){
        global $j_month_name,$j_week_name, $hejri_error;
        if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})", $date, $datetime)) {
                $hejriDate = gregorian_to_jalali($datetime[1],$datetime[2],$datetime[3]);
                $datetime = $hejriDate[2]." ".$j_month_name[$hejriDate[1]]."، ".$hejriDate[0]." ".$datetime[4].":".$datetime[5];
                return $datetime;
        } else {
                return $hejri_error;
        }
}

//hejri to gregorian functions
function tep_back2format($date){
    global $hejri_error;
    if (ereg ("([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})", $date, $datetime)) {
             $gredate = jalali_to_gregorian($datetime[1],$datetime[2],$datetime[3]);
             $date = sprintf("%02d",$gredate[1]) ."/". sprintf("%02d",$gredate[2]) ."/".$gredate[0];
             return $date;
    } else {
             return $hejri_error;
    }
}

//creates a pull_down list of hejri month
function tep_get_hejri_month($name, $selected = '') {
        global $j_month_name;
        $hejri_month_array = array(array('id' => '0', 'text' => PULL_DOWN_DEFAULT));

        for ($i=1, $n=sizeof($j_month_name); $i<$n; $i++) {
                $hejri_month_array[] = array('id' => $i, 'text' => $j_month_name[$i]);
        }

        return tep_draw_pull_down_menu($name, $hejri_month_array, $selected);
}

//creates a pull_down list of month days
function tep_get_hejri_day($month, $name, $selected = '') {
        global $j_days_in_month;
        $hejri_day_array = array(array('id' => '1', 'text' => '01'));
        $n = $j_days_in_month[$month-1];
        for($i=2; $i<=$n; $i++) {
                $hejri_day_array[] = array('id' => $i, 'text' => sprintf("%02d",$i));
        }

        return tep_draw_pull_down_menu($name, $hejri_day_array, $selected);
}

// header date
function tep_hejri_header($date){
        global $j_month_name,$j_week_name, $hejri_error;
        if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})", $date, $datetime)) {
                $hejriDate = gregorian_to_jalali($datetime[1],$datetime[2],$datetime[3]);
                $tep_hd = jalali_week_day($hejriDate[0],$hejriDate[1],$hejriDate[2]);
                $datetime = "امروز " . $j_week_name[$tep_hd]."، ".$hejriDate[2]." ".$j_month_name[$hejriDate[1]]." ماه ".$hejriDate[0]." ";
                return $datetime;
        } else {
                return $hejri_error;
        }
}


?>