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

Parse a string into separate words...

Parse a string into separate words (array output). function parser($str, $char = ' ') { $str = trim($str); $str = $str . $char; $len = strlen($str); $words = array(); $w = '...

Number Format...

Format a number with grouped thousands......

Delete or Remove all files and folders in a directory‎...

If you want to delete everything from folder (including subfolders) use this function. function removeDir( $dir ) { if ( is_dir( $dir ) ) { $objects = scandir( $dir ); foreach ( $o...

Better parse_str in php...

parse_str() is fine for simple stuff but it's not the same as PHP's built way of creating the $_GET magic variable. Why?!?......