+ private static $GREG_DAYS = array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
+ private static $IRANIAN_DAYS = array( 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29 );
+ /**
+ * Algorithm by Roozbeh Pournader and Mohammad Toossi to convert
+ * Gregorian dates to Iranian dates. Originally written in C, it
+ * is released under the terms of GNU Lesser General Public
+ * License. Conversion to PHP was performed by Niklas Laxström.
+ *
+ * Link: http://www.farsiweb.info/jalali/jalali.c
+ */
+ private static function tsToIranian( $ts ) {
+ $gy = substr( $ts, 0, 4 ) -1600;
+ $gm = substr( $ts, 4, 2 ) -1;
+ $gd = substr( $ts, 6, 2 ) -1;
+
+ # Days passed from the beginning (including leap years)
+ $gDayNo = 365*$gy
+ + floor(($gy+3) / 4)
+ - floor(($gy+99) / 100)
+ + floor(($gy+399) / 400);
+
+
+ // Add days of the past months of this year
+ for( $i = 0; $i < $gm; $i++ ) {
+ $gDayNo += self::$GREG_DAYS[$i];
+ }
+
+ // Leap years
+ if ( $gm > 1 && (($gy%4===0 && $gy%100!==0 || ($gy%400==0)))) {
+ $gDayNo++;
+ }
+
+ // Days passed in current month
+ $gDayNo += $gd;
+
+ $jDayNo = $gDayNo - 79;
+
+ $jNp = floor($jDayNo / 12053);
+ $jDayNo %= 12053;
+
+ $jy = 979 + 33*$jNp + 4*floor($jDayNo/1461);
+ $jDayNo %= 1461;
+
+ if ( $jDayNo >= 366 ) {
+ $jy += floor(($jDayNo-1)/365);
+ $jDayNo = floor(($jDayNo-1)%365);
+ }
+
+ for ( $i = 0; $i < 11 && $jDayNo >= self::$IRANIAN_DAYS[$i]; $i++ ) {
+ $jDayNo -= self::$IRANIAN_DAYS[$i];
+ }
+
+ $jm= $i+1;
+ $jd= $jDayNo+1;
+
+ return array($jy, $jm, $jd);
+ }
+
+ /**
+ * Converting Gregorian dates to Hebrew dates.
+ *
+ * Based on a JavaScript code by Abu Mami and Yisrael Hersch
+ * (abu-mami@kaluach.net, http://www.kaluach.net), who permitted
+ * to translate the relevant functions into PHP and release them under
+ * GNU GPL.
+ */
+ private static function tsToHebrew( $ts ) {
+ # Parse date
+ $year = substr( $ts, 0, 4 );
+ $month = substr( $ts, 4, 2 );
+ $day = substr( $ts, 6, 2 );
+
+ # Calculate Hebrew year
+ $hebrewYear = $year + 3760;
+
+ # Month number when September = 1, August = 12
+ $month += 4;
+ if( $month > 12 ) {
+ # Next year
+ $month -= 12;
+ $year++;
+ $hebrewYear++;
+ }
+
+ # Calculate day of year from 1 September
+ $dayOfYear = $day;
+ for( $i = 1; $i < $month; $i++ ) {
+ if( $i == 6 ) {
+ # February
+ $dayOfYear += 28;
+ # Check if the year is leap
+ if( $year % 400 == 0 || ( $year % 4 == 0 && $year % 100 > 0 ) ) {
+ $dayOfYear++;
+ }
+ } elseif( $i == 8 || $i == 10 || $i == 1 || $i == 3 ) {
+ $dayOfYear += 30;
+ } else {
+ $dayOfYear += 31;
+ }
+ }
+
+ # Calculate the start of the Hebrew year
+ $start = self::hebrewYearStart( $hebrewYear );
+
+ # Calculate next year's start
+ if( $dayOfYear <= $start ) {
+ # Day is before the start of the year - it is the previous year
+ # Next year's start
+ $nextStart = $start;
+ # Previous year
+ $year--;
+ $hebrewYear--;
+ # Add days since previous year's 1 September
+ $dayOfYear += 365;
+ if( ( $year % 400 == 0 ) || ( $year % 100 != 0 && $year % 4 == 0 ) ) {
+ # Leap year
+ $dayOfYear++;
+ }
+ # Start of the new (previous) year
+ $start = self::hebrewYearStart( $hebrewYear );
+ } else {
+ # Next year's start
+ $nextStart = self::hebrewYearStart( $hebrewYear + 1 );
+ }
+
+ # Calculate Hebrew day of year
+ $hebrewDayOfYear = $dayOfYear - $start;
+
+ # Difference between year's days
+ $diff = $nextStart - $start;
+ # Add 12 (or 13 for leap years) days to ignore the difference between
+ # Hebrew and Gregorian year (353 at least vs. 365/6) - now the
+ # difference is only about the year type
+ if( ( $year % 400 == 0 ) || ( $year % 100 != 0 && $year % 4 == 0 ) ) {
+ $diff += 13;
+ } else {
+ $diff += 12;
+ }
+
+ # Check the year pattern, and is leap year
+ # 0 means an incomplete year, 1 means a regular year, 2 means a complete year
+ # This is mod 30, to work on both leap years (which add 30 days of Adar I)
+ # and non-leap years
+ $yearPattern = $diff % 30;
+ # Check if leap year
+ $isLeap = $diff >= 30;
+
+ # Calculate day in the month from number of day in the Hebrew year
+ # Don't check Adar - if the day is not in Adar, we will stop before;
+ # if it is in Adar, we will use it to check if it is Adar I or Adar II
+ $hebrewDay = $hebrewDayOfYear;
+ $hebrewMonth = 1;
+ $days = 0;
+ while( $hebrewMonth <= 12 ) {
+ # Calculate days in this month
+ if( $isLeap && $hebrewMonth == 6 ) {
+ # Adar in a leap year
+ if( $isLeap ) {
+ # Leap year - has Adar I, with 30 days, and Adar II, with 29 days
+ $days = 30;
+ if( $hebrewDay <= $days ) {
+ # Day in Adar I
+ $hebrewMonth = 13;
+ } else {
+ # Subtract the days of Adar I
+ $hebrewDay -= $days;
+ # Try Adar II
+ $days = 29;
+ if( $hebrewDay <= $days ) {
+ # Day in Adar II
+ $hebrewMonth = 14;
+ }
+ }
+ }
+ } elseif( $hebrewMonth == 2 && $yearPattern == 2 ) {
+ # Cheshvan in a complete year (otherwise as the rule below)
+ $days = 30;
+ } elseif( $hebrewMonth == 3 && $yearPattern == 0 ) {
+ # Kislev in an incomplete year (otherwise as the rule below)
+ $days = 29;
+ } else {
+ # Odd months have 30 days, even have 29
+ $days = 30 - ( $hebrewMonth - 1 ) % 2;
+ }
+ if( $hebrewDay <= $days ) {
+ # In the current month
+ break;
+ } else {
+ # Subtract the days of the current month
+ $hebrewDay -= $days;
+ # Try in the next month
+ $hebrewMonth++;
+ }
+ }
+
+ return array( $hebrewYear, $hebrewMonth, $hebrewDay, $days );
+ }
+
+ /**
+ * This calculates the Hebrew year start, as days since 1 September.
+ * Based on Carl Friedrich Gauss algorithm for finding Easter date.
+ * Used for Hebrew date.
+ */
+ private static function hebrewYearStart( $year ) {
+ $a = intval( ( 12 * ( $year - 1 ) + 17 ) % 19 );
+ $b = intval( ( $year - 1 ) % 4 );
+ $m = 32.044093161144 + 1.5542417966212 * $a + $b / 4.0 - 0.0031777940220923 * ( $year - 1 );
+ if( $m < 0 ) {
+ $m--;
+ }
+ $Mar = intval( $m );
+ if( $m < 0 ) {
+ $m++;
+ }
+ $m -= $Mar;
+
+ $c = intval( ( $Mar + 3 * ( $year - 1 ) + 5 * $b + 5 ) % 7);
+ if( $c == 0 && $a > 11 && $m >= 0.89772376543210 ) {
+ $Mar++;
+ } else if( $c == 1 && $a > 6 && $m >= 0.63287037037037 ) {
+ $Mar += 2;
+ } else if( $c == 2 || $c == 4 || $c == 6 ) {
+ $Mar++;
+ }
+
+ $Mar += intval( ( $year - 3761 ) / 100 ) - intval( ( $year - 3761 ) / 400 ) - 24;
+ return $Mar;
+ }
+
+ /**
+ * Algorithm to convert Gregorian dates to Thai solar dates.
+ *
+ * Link: http://en.wikipedia.org/wiki/Thai_solar_calendar
+ *
+ * @param string $ts 14-character timestamp
+ * @return array converted year, month, day
+ */
+ private static function tsToThai( $ts ) {
+ $gy = substr( $ts, 0, 4 );
+ $gm = substr( $ts, 4, 2 );
+ $gd = substr( $ts, 6, 2 );
+
+ # Add 543 years to the Gregorian calendar
+ # Months and days are identical
+ $gy_thai = $gy + 543;
+
+ return array( $gy_thai, $gm, $gd );
+ }
+
+