Gregorian to Persian Date Convertor

5 Years ago(Updated in 5 Years ago) PHP(Functions) 0

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

PHP subwords() Function...

Function subwords, gets words by max num....

Create directory function...

Create directory if not exists. function createDir( $dir ) { if ( !is_dir( $dir ) ) { mkdir( $dir, 0777 ); return true; } return false; }...

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

Comments

No comment here.
captcha image reload