PHP Conference Japan 2024

unixtojd

(PHP 4, PHP 5, PHP 7, PHP 8)

unixtojd將 Unix 時間戳記轉換為儒略日

說明

unixtojd(?int $timestamp = null): int|false

傳回 Unix timestamp (自 1970 年 1 月 1 日起的秒數) 的儒略日,如果未提供 timestamp,則傳回當天的儒略日。無論如何,時間都被視為當地時間(非 UTC)。

參數

timestamp

要轉換的 Unix 時間戳記。

傳回值

儒略日編號,為整數;失敗時傳回 false

更新日誌

版本 說明
8.0.0 timestamp 現在可以為 null。

另請參見

  • jdtounix() - 將儒略日轉換為 Unix 時間戳記

新增註釋

使用者貢獻的註釋 6 則註釋

fabio at llgp dot org
18 年前
如果您需要一種簡單的方法將 Unix 時間戳記轉換為小數儒略日,您可以使用

$julianDay = $unixTimeStamp / 86400 + 2440587.5;

86400 是一天中的秒數;
2440587.5 是 1970 年 1 月 1 日 0:00 UTC 的儒略日。
匿名
18 年前
此函數明確指出它返回儒略日,而不是儒略日 + 時間。

如果您想要包含時間,您必須執行以下操作,例如

$t=time();
$jd=unixtojd($t)+($t%60*60*24)/60*60*24;
unixtojd at isslow dot com
9 個月前
unixtojd 速度很慢。
直接算術計算速度更快,並且仍然與原始 unixtojd 一致。

當 $timestamp 為 null 時,您可以自由地對 $timestamp 添加測試以將其設定為 time()。

function fast_unixtojd($timestamp){
return intval($timestamp / 86400 + 2440588);
}

$time = time();
$t_unixtojd = 0;
$t_fast_unixtojd = 0;
for ($t = $time - 240 * 3600; $t < $time; $t++) {
$time1 = microtime(true);
$a = unixtojd($t);
$time2 = microtime(true);
$b = fast_unixtojd($t);
$time3 = microtime(true);
if ($a != $b) {
echo "$a $b $t\n";
break;
}
$t_unixtojd += $time2 - $time1;
$t_fast_unixtojd += $time3 - $time2;
}
echo "unixtojd: $t_unixtojd sec\nfast_unixtojd: $t_fast_unixtojd sec\n";

unixtojd: 0.42854166030884 秒
fast_unixtojd: 0.13218021392822 秒
hrabi at linuxwaves dot com
17 年前
根據 http://www.decimaltime.hynes.net/dates.html#jd 並閱讀此頁面上的「X. 日曆函數」,似乎 php 的「jd」正是指「編年儒略日」(它是否應該被命名為 cjd,並且主要被嚴格提及 - 不是嗎?),用於日曆系統之間的轉換。那麼它沒問題(但我不認為不完整的說明文件在這裡非常令人困惑)。
即使如此,cJD 還是被調整為當地時間,所以... 我現在有點搞混了,所以沒別的了 :-).
hrabi at linuxwaves dot com
17 年前
這無法使用。儒略日從中午開始,而不是午夜。最好使用 Fabio 的解決方案(然而閏秒存在潛在問題)。

<?php
function mmd($txt, $str_time) {
$t = strtotime($str_time);
$j = unixtojd($t);
$s = gmstrftime('%D %T %Z', $t);
$j_fabio = $t / 86400 + 2440587.5;

printf("${txt} => (%s) %s, %s U, %s J, or %s J<br>\n", $str_time, $s, $t, $j, $j_fabio);
}

//$xt = strtotime("1.1.1970 15:00.00 GMT");
$sam = "9.10.1995 02:00.01 GMT";
$spm = "9.10.1995 22:00.01 GMT";

// $spm 的 unixtojd 傳回 2450000 (正確),但 $sam 也傳回 2450000!(這是錯誤的)。
mmd("am", $sam); // 應該為 2449999 (+ 0.58334)
mmd("pm", $spm); // 應該為 2450000 (+ 0.41668)
?>

參考
Unix 時間,以及 UTC、TAI、ntp 等問題: http://en.wikipedia.org/wiki/Unix_time
儒略日轉換器: http://aa.usno.navy.mil/data/docs/JulianDate.html
歷史概覽: http://parris.josh.com.au/humour/work/17Nov1858.shtml
johnston at capsaicin dot ca
21 年前
另請注意,epoch 是 UTC 時間(epoch 是一個特定的時間點 - epoch 並不會因時區而異),因此請注意時區的複雜性。
To Top