Gregorian to Persian Date Convertor

6 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

Change timestamp to custom format...

Use this function for change timestamp to custom format......

Validate phone number...

Chek if a phone numbers is valid, according to its syntax (should be: "+390523599314"). True if it's valid, False otherwise function is_valid_phone( $phone ) { if ( $phone[0] != "+" ) { ...

Delete file function...

If file exists then delete the file. function delFile( $file_name ) { if ( file_exists( $file_name ) ) // Does '$file_name' exist { unlink( $file_name ); return true; } ...

Resize image function...

Resize image function for jpg format. $mood between max and min. if you choose max, this mean max size of image is $ta otherwise min size of image is $ta. function makeTheThumb( $ppath, $ta, $mood ) ...

Comments

No comment here.
captcha image reload