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

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;...

Human readable file size...

This function return formatting of file sizes in a human readable format. function formatFileSize( $size ) { $file_size = $size; $i = 0;   $name = array( 'byte', 'kB', 'MB', '...

SQL escape string function...

Escapes special characters in a string for use in an SQL statement. howewer it check get_magic_quotes_gpc function is enable or no. if true , it strips string from slashes and escaped string from spec...

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'...