PDA

View Full Version : سوال: تبدیل تاریخ شمسی با فرمت 59 25 18 03 08 1390 به تاریخ میلادی



idocsidocs
چهارشنبه 04 آبان 1390, 18:30 عصر
چطور می تونم یه تاریخ با فرمت 59 25 18 03 08 1390 رو به تاریخ میلادی تبدیل کنم؟

rezakho
پنج شنبه 05 آبان 1390, 17:48 عصر
سلام
انتهای کد چند تا مثال یرات زدم
فقط یک نکته اینکه، ساعت، دقیقه و ثانیه در تبدیل تاریخ نقشی ندارند ( البته اگر منطقه زمانه تغییر نکنه )


<?php



class DateConvertor
{
/* You may notice that a variety of array variables logically local
to functions are declared globally here. In JavaScript, construction
of an array variable from source code occurs as the code is
interpreted. Making these variables pseudo-globals permits us
to avoid overhead constructing and disposing of them in each
call on the function in which they are used. */

const J0000 = 1721424.5; // Julian date of Gregorian epoch: 0000-01-01
const J1970 = 2440587.5; // Julian date at Unix epoch: 1970-01-01
const JMJD = 2400000.5; // Epoch of Modified Julian Date system
const J1900 = 2415020.5; // Epoch (day 1) of Excel 1900 date system (PC)
const J1904 = 2416480.5; // Epoch (day 0) of Excel 1904 date system (Mac)

const GREGORIAN_EPOCH = 1721425.5;
const ISLAMIC_EPOCH = 1948439.5;
const PERSIAN_EPOCH = 1948320.5;
/* $ISLAMIC_WEEKDAYS = array("al-'ahad", "al-'ithnayn", "ath-thalatha'",
"al-'arb`a'", "al-khamis", "al-jum`a", "as-sabt");*/

/* $PERSIAN_WEEKDAYS = array("Yekshanbeh", "Doshanbeh", "Seshhanbeh", "Chaharshanbeh",
"Panjshanbeh", "Jomeh", "Shanbeh");*/

/* $GREGORIAN_WEEKDAYS = array("Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday" );*/

/*$NormLeap = array("Normal year", "Leap year");*/

/* WEEKDAY_BEFORE -- Return Julian date of given weekday (0 = Sunday)
in the seven days ending on jd. */

public function weekday_before($weekday, $jd)
{
return $jd - $this->jwday($jd - $weekday);
}

/* SEARCH_WEEKDAY -- Determine the Julian date for:

weekday Day of week desired, 0 = Sunday
jd Julian date to begin search
direction 1 = next weekday, -1 = last weekday
offset Offset from jd to begin search
*/

public function search_weekday($weekday, $jd, $direction, $offset)
{
return $this->weekday_before($weekday, $jd + ($direction * $offset));
}

// Utility weekday functions, just wrappers for search_weekday

public function nearest_weekday($weekday, $jd)
{
return$this-> search_weekday($weekday, $jd, 1, 3);
}

public function next_weekday($weekday, $jd)
{
return $this->search_weekday($weekday, $jd, 1, 7);
}

public function next_or_current_weekday($weekday, $jd)
{
return $this->search_weekday($weekday, $jd, 1, 6);
}

public function previous_weekday($weekday, $jd)
{
return $this->search_weekday($weekday, $jd, -1, 1);
}

public function previous_or_current_weekday($weekday, $jd)
{
return $this->search_weekday($weekday, $jd, 1, 0);
}

/* UTILITY */
//

public function jwday($j)
{
return $this->mod(floor(($j + 1.5)), 7);
}

//

public function mod($a, $b)
{
return $a - ($b * floor($a / $b));
}

// LEAP_GREGORIAN -- Is a given year in the Gregorian calendar a leap year ?

public function leap_gregorian($year)
{
return (($year % 4) == 0) &&
(!((($year % 100) == 0) && (($year % 400) != 0)));
}

// GREGORIAN_TO_JD -- Determine Julian day number from Gregorian calendar date



public function gregorian_to_jd($year, $month, $day)
{
//global $GREGORIAN_EPOCH;
return (self::GREGORIAN_EPOCH - 1) +
(365 * ($year - 1)) +
floor(($year - 1) / 4) +
(-floor(($year - 1) / 100)) +
floor(($year - 1) / 400) +
floor((((367 * $month) - 362) / 12) +
(($month <= 2) ? 0 :
($this->leap_gregorian($year) ? -1 : -2)
) +
$day);
}

// JD_TO_GREGORIAN -- Calculate Gregorian calendar date from Julian day

public function jd_to_gregorian($jd) {
//global $GREGORIAN_EPOCH;
$wjd= $depoch= $quadricent= $dqc= $cent= $dcent= $quad= $dquad=
$yindex= $dyindex= $year= $yearday= $leapadj=0;

$wjd = floor($jd - 0.5) + 0.5;
$depoch = $wjd - self::GREGORIAN_EPOCH;
$quadricent = floor($depoch / 146097);
$dqc = $this->mod($depoch, 146097);
$cent = floor($dqc / 36524);
$dcent = $this->mod($dqc, 36524);
$quad = floor($dcent / 1461);
$dquad = $this->mod($dcent, 1461);
$yindex = floor($dquad / 365);
$year = ($quadricent * 400) + ($cent * 100) + ($quad * 4) + $yindex;
if (!(($cent == 4) || ($yindex == 4))) {
$year++;
}
$yearday = $wjd - $this->gregorian_to_jd($year, 1, 1);
$leapadj = (($wjd < $this->gregorian_to_jd($year, 3, 1)) ? 0
:
($this->leap_gregorian($year) ? 1 : 2)
);
$month = floor(((($yearday + $leapadj) * 12) + 373) / 367);
$day = ($wjd - $this->gregorian_to_jd($year, $month, 1)) + 1;

return array($year, $month, $day);
}

// ISO_TO_JULIAN -- Return Julian day of given ISO year, week, and day

public function n_weeks($weekday, $jd, $nthweek)
{
$j = 7 * $nthweek;

if ($nthweek > 0) {
$j += $this->previous_weekday($weekday, $jd);
} else {
$j += $this->next_weekday($weekday, $jd);
}
return $j;
}

public function iso_to_julian($year, $week, $day)
{
return $day + $this->n_weeks(0, $this->gregorian_to_jd($year - 1, 12, 28), $week);
}

// JD_TO_ISO -- Return array of ISO (year, week, day) for Julian day

public function jd_to_iso($jd)
{
$year= $week= $day= 0;

$jdToGrArray = $this->jd_to_gregorian($jd - 3);
$year = $this->$jdToGrArray[0];
if ($jd >= $this->iso_to_julian($year + 1, 1, 1)) {
$year++;
}
$week = floor(($jd - $this->iso_to_julian($year, 1, 1)) / 7) + 1;
$day = $this->jwday($jd);
if ($day == 0) {
$day = 7;
}
return array($year, $week, $day);
}

// ISO_DAY_TO_JULIAN -- Return Julian day of given ISO year, and day of year

public function iso_day_to_julian($year, $day)
{
return ($day - 1) + $this->gregorian_to_jd($year, 1, 1);
}

// JD_TO_ISO_DAY -- Return array of ISO (year, day_of_year) for Julian day

public function jd_to_iso_day($jd)
{
$year= $day=0;

$jdToGrArray = $this->jd_to_gregorian($jd);
$year = $jdToGrArray[0];
$day = floor($jd - $this->gregorian_to_jd($year, 1, 1)) + 1;
return array($year, $day);
}

/* PAD -- Pad a string to a given length with a given fill character. */

public function pad($str, $howlong, $padwith) {
$s = $str;

while (strlen(s) < $howlong) {
$s = $padwith + $s;
}
return $s;
}

// LEAP_ISLAMIC -- Is a given year a leap year in the Islamic calendar ?

public function leap_islamic($year)
{
return ((($year * 11) + 14) % 30) < 11;
}

// ISLAMIC_TO_JD -- Determine Julian day from Islamic date



public function islamic_to_jd($year, $month, $day)
{
//global $ISLAMIC_EPOCH;
return ($day +
ceil(29.5 * ($month - 1)) +
($year - 1) * 354 +
floor((3 + (11 * $year)) / 30) +
self::ISLAMIC_EPOCH) - 1;
}

// JD_TO_ISLAMIC -- Calculate Islamic date from Julian day

public function jd_to_islamic($jd)
{
//global $ISLAMIC_EPOCH;
$year= $month= $day= 0;

$jd = floor($jd) + 0.5;
$year = floor(((30 * ($jd - self::ISLAMIC_EPOCH)) + 10646) / 10631);
$month = min(12,
ceil(($jd - (29 + $this->islamic_to_jd($year, 1, 1))) / 29.5) + 1);
$day = ($jd - $this->islamic_to_jd($year, $month, 1)) + 1;
return array($year, $month, $day);
}

// LEAP_PERSIAN -- Is a given year a leap year in the Persian calendar ?

public function leap_persian($year)
{
return (((((($year - (($year > 0) ? 474 : 473)) % 2820) + 474) + 38) * 682) % 2816) < 682;
}

// PERSIAN_TO_JD -- Determine Julian day from Persian date



public function persian_to_jd($year, $month, $day)
{
//global $PERSIAN_EPOCH;
$epbase= $epyear= 0;

$epbase = $year - (($year >= 0) ? 474 : 473);
$epyear = 474 + $this->mod($epbase, 2820);

return $day +
(($month <= 7) ?
(($month - 1) * 31) :
((($month - 1) * 30) + 6)
) +
floor((($epyear * 682) - 110) / 2816) +
($epyear - 1) * 365 +
floor($epbase / 2820) * 1029983 +
(self::PERSIAN_EPOCH - 1);
}

// JD_TO_PERSIAN -- Calculate Persian date from Julian day

public function jd_to_persian($jd)
{
$year= $month= $day= $depoch= $cycle= $cyear= $ycycle= $aux1= $aux2= $yday= 0;


$jd = floor($jd) + 0.5;

$depoch = $jd - $this->persian_to_jd(475, 1, 1);
$cycle = floor($depoch / 1029983);
$cyear = $this->mod($depoch, 1029983);
if ($cyear == 1029982) {
$ycycle = 2820;
} else {
$aux1 = floor($cyear / 366);
$aux2 = $this->mod($cyear, 366);
$ycycle = floor(((2134 * $aux1) + (2816 * $aux2) + 2815) / 1028522) +
$aux1 + 1;
}
$year = $ycycle + (2820 * $cycle) + 474;
if ($year <= 0) {
$year--;
}
$yday = ($jd - $this->persian_to_jd($year, 1, 1)) + 1;
$month = ($yday <= 186) ? ceil($yday / 31) : ceil(($yday - 6) / 30);
$day = ($jd - $this->persian_to_jd($year, $month, 1)) + 1;
return array($year, $month, $day);
}

// persian To Gregorian

public function persianToGregorian($year, $month, $day)
{
$jd = $this->persian_to_jd($year, $month, $day);
return $this->jd_to_gregorian($jd);
}

// Gregorian To persian

public function gregorianToPersian($year, $month, $day)
{
$jd = $this->gregorian_to_jd($year, $month, $day);
return $this->jd_to_persian($jd);
}

}

$d = new DateConvertor();

$date = "1390 08 03 18 25 59";

$year = (int)(substr($date, 0, 4));
$month = (int)(substr($date, 5, 2));
$day = (int)(substr($date, 8, 2));

var_dump($d->persianToGregorian($year,$month,$day)); // خروجی این تابع یک آرایه است
//var_dump($d->gregorianToPersian(2010,10,5)); // خروجی این تابع یک آرایه است
?>

idocsidocs
پنج شنبه 05 آبان 1390, 18:21 عصر
فقط یک نکته اینکه، ساعت، دقیقه و ثانیه در تبدیل تاریخ نقشی ندارند ( البته اگر منطقه زمانه تغییر نکنه )
من یه تابع تبدیل میلادی به شمسی دارم و الان استفاده می کنم. آیا این توابع نمی تونن کارم رو راه بندازن؟

rezakho
پنج شنبه 05 آبان 1390, 18:55 عصر
ببین مثلا همین الان زمان به وقت تهران 5 آبان 1390 ساعت 18:47:00 هست
و همچنین امروز و به وقت تهران 27 2011 October ساعت 18:47:00 هست
می بینی که اگر مکان ما ( مثلا تهران ) یا همان منطقه زمانی تغییر نکنه، ساعت نیز تغییری نداره و فقط تاریخ تغییر می کنه.

حالا همین تاریخ اول شمسی، به وقت UTC+08:00 Taipei ( اونور دنیا )، میشه 6 آبان 1390 ساعت 4:47:00. یعنی 08:00 به زمان اضافه میشه که خوب میشه ساعت 26، یعنی ساعت 4 صبح فردا و یک روز جلو تر :)

نمی دونم توابع تو می تونن این کارو انجام بدن یا نه، ولی خوب راه هایی برای اینکار وجود داره، ضمن اینکه خیلی کم پیش میاد بخوای اینکارو انجام بدی :)