Gregorian to Persian Date Convertor

Gregorian to Persian Date Convertor.

function gLeapYear( $year )
{
   if ( ( $year % 4 == 0 ) and ( ( $year % 100 != 0 ) or ( $year % 400 == 0 ) ) )
      return true;
   else
      return false;
}

function sLeapYear( $year )
{
   $ary = array( 1, 5, 9, 13, 17, 22, 26, 30 );
   $result = false;
   $b = $year % 33;
   if ( in_array( $b, $ary ) )
      $result = true;
   return $result;
}

function shamsiDate( $gyear, $gmonth, $gday )
{
   $_gl = array( 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 );
   $_g = array( 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 );
   $_smNames = array( 1 => 'فروردین', 2 => 'اردیبهشت', 3 => 'خرداد', 4 => 'تیر', 5 => 'مرداد', 6 => 'شهریور', 7 => 'مهر', 8 => 'آبان', 9 => 'آذر', 10 => 'دی', 11 => 'بهمن', 12 => 'اسفند' );
   $result = array();
   $deydiffjan = 10;
   if ( gLeapYear( $gyear - 1 ) )
      $deydiffjan = 11;
   if ( gLeapYear( $gyear ) )
      $gd = $_gl[$gmonth - 1] + $gday;
   else
      $gd = $_g[$gmonth - 1] + $gday;
   if ( $gd > 79 ) {
      $sy = $gyear - 621;
      $gd = $gd - 79;
      if ( $gd <= 186 ) {
         $gmod = $gd % 31;
         switch ( $gmod ) {
            case 0:
               $sd = 31;
               $sm = ( int )( $gd / 31 );
               break;
            default:
               $sd = $gmod;
               $sm = ( int )( $gd / 31 ) + 1;
         }
      }
      else {
         $gd = $gd - 186;
         $gmod = $gd % 30;
         switch ( $gmod ) {
            case 0:
               $sd = 30;
               $sm = ( int )( $gd / 30 ) + 6;
               break;
            default:
               $sd = $gmod;
               $sm = ( int )( $gd / 30 ) + 7;
         }
      }
   }
   else {
      $sy = $gyear - 622;
      $gd = $gd + $deydiffjan;
      $gmod = $gd % 30;
      switch ( $gmod ) {
         case 0:
            $sd = 30;
            $sm = ( int )( $gd / 30 ) + 9;
            break;
         default:
            $sd = $gmod;
            $sm = ( int )( $gd / 30 ) + 10;
      }
   }

   return "$sd $_smNames[$sm] $sy";
}

echo shamsiDate( 2013, 12, 14 ); //out =  23 آذر 1392

Tags

No tag here.

Recommended pages

alphaID - Translates a number to a short alhanumeric version...

Translates a number to a short alhanumeric version. e.g.: 9007199254740989 --> PpQXn7COf In most cases this is better than totally random ID generators because this can easily avoid duplicate ID's. Fo...

UTF8 string length counter...

The best way to determine the character count of a UTF8 string. function strlen_utf8( $str ) { return mb_strlen( $str, 'UTF-8' ); }   //or... function strlen_utf8( $str ) { $c = 0;...

Email Address Checker...

This function check if an e-mail address is valid or not. function valid_email($email) { return preg_match('/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/', $email);...

Encrypt and Decrypt function with different outputs...

Encrypt function return different value with each execute but decrypt function return input value. for example: first execute: encrypt('hello') => 'axxdfg34', decrypt('axxdfg34') return 'hello'...