unixtojd

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

unixtojd β€” ΠŸΠ΅Ρ€Π΅Π²ΠΎΠ΄ΠΈΡ‚ ΠΌΠ΅Ρ‚ΠΊΡƒ Π²Ρ€Π΅ΠΌΠ΅Π½ΠΈ Unix Π² юлианский дСнь

ОписаниС

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

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ юлианский дСнь для Π·Π°Π΄Π°Π½Π½ΠΎΠΉ ΠΌΠ΅Ρ‚ΠΊΠΈ Π²Ρ€Π΅ΠΌΠ΅Π½ΠΈ Unix timestamp (количСство сСкунд с 1.1.1970) Π»ΠΈΠ±ΠΎ для Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π³ΠΎ Π²Ρ€Π΅ΠΌΠ΅Π½ΠΈ, Ссли Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚ timestamp Π½Π΅ Π·Π°Π΄Π°Π½. Π’ любом случаС, врСмя рассматриваСтся ΠΊΠ°ΠΊ мСстноС врСмя (Π½Π΅ UTC).

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

timestamp

ΠœΠ΅Ρ‚ΠΊΠ° Π²Ρ€Π΅ΠΌΠ΅Π½ΠΈ Unix для прСобразования.

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Число Π΄Π½Π΅ΠΉ Π² юлианском лСтоисчислСнии ΠΈΠ»ΠΈ false, Ссли Π²ΠΎΠ·Π½ΠΈΠΊΠ»Π° ошибка.

Бписок измСнСний

ВСрсия ОписаниС
8.0.0 timestamp Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ допускаСт Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ null.

Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅

  • jdtounix() - ΠŸΠ΅Ρ€Π΅Π²ΠΎΠ΄ΠΈΡ‚ число Π΄Π½Π΅ΠΉ Π² юлианском лСтоисчислСнии Π² ΠΌΠ΅Ρ‚ΠΊΡƒ Π²Ρ€Π΅ΠΌΠ΅Π½ΠΈ Unix
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 6 notes

up
3
fabio at llgp dot org ΒΆ
19 years ago
If you need an easy way to convert an unix timestamp to a decimal julian day you can use:

$julianDay = $unixTimeStamp / 86400 + 2440587.5;

86400 is the number of seconds in a day;
2440587.5 is the julian day at 1/1/1970 0:00 UTC.
up
2
Anonymous ΒΆ
19 years ago
Its clearly stated that this function returns the Julian Day, not Julian Day + time.

If you want the time with it you will have to do something like:

$t=time();
$jd=unixtojd($t)+($t%60*60*24)/60*60*24;
up
0
unixtojd at isslow dot com ΒΆ
2 years ago
unixtojd is slow.
Direct arithmetics calculations are faster and still coherent with original unixtojd.

Feel free to add a test on $timestamp to set it to time() when $timestamp is null.

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 sec
fast_unixtojd: 0.13218021392822 sec
up
0
hrabi at linuxwaves dot com ΒΆ
19 years ago
according to http://www.decimaltime.hynes.net/dates.html#jd and reading "X. Calendar Functions" on this side, it seems that php "jd" is precisely mean as "Chronological Julian Day" (should it be named cjd, and primarily strictly mentioned - isn't it?), used for covnersion between calendar systems. Than it's ok (but Incomplete manual is strongly confusing here IMHO).
Even that, cJD is adjusted to a local time, so... I am rather babeled now, so nothing else :-).
up
0
hrabi at linuxwaves dot com ΒΆ
19 years ago
This is unusable. Julian Day start at noon, not midnight. It's better to use Fabio solution (however there is a lurk problem with leap second).

<?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";

// unixtojd for $spm returns 2450000 (OK), but for $sam returns 2450000 too! (it is wrong).
mmd("am", $sam);  // should be 2449999 (+ 0.58334)
mmd("pm", $spm);  // should be 2450000 (+ 0.41668)
?>

reference
unix time, and UTC, TAI, ntp, ... problems: http://en.wikipedia.org/wiki/Unix_time
Julian Date Converter: http://aa.usno.navy.mil/data/docs/JulianDate.html
history overview: http://parris.josh.com.au/humour/work/17Nov1858.shtml
up
0
johnston at capsaicin dot ca ΒΆ
22 years ago
Also note that epoch is in UTC time (epoch is a specific point in time - epoch is not different for every time zone), so be aware of timezone complexities.