Some people might find it useful to use a function that takes in dates formatted by ISO8601 standards (yyyy-mm-dd):
<?php function daysBetweenDate($from, $till) {
/*
*This function will calculate the difference between two given dates.
*
*Please input time by ISO 8601 standards (yyyy-mm-dd).
*i.e: daysBetweenDate('2009-01-01', '2010-01-01');
*This will return 365.
*
*Author: brian [at] slaapkop [dot] net
*May 5th 2010
*/
if($till < $from) {
trigger_error("The date till is before the date from", E_USER_NOTICE);
}
//Explode date since gregoriantojd() requires mm, dd, yyyy input;
$from = explode('-', $from);
$till = explode('-', $till);
//Calculate date to Julian Day Count with freshly created array $from.
$from = gregoriantojd($from[1], $from[2], $from[0])."<br />";
//Calculate date to Julian Day Count with freshly created array $till.
$till = gregoriantojd($till[1], $till[2], $till[0])."<br />";
//Substract the days $till (largest number) from $from (smallest number) to get the amount of days
$days = ($till - $from);
//Return the number of days.
return $days;
//Isn't it sad how my comments use more lines than the actual code?
}
?>
GregorianToJD
(PHP 4, PHP 5)
GregorianToJD — Converte uma data gregoriana para o "Julian Day Count"
Descrição
Escala válida para o Calendário Gregoriano: 4714 A.C. to 9999 D.C.
Entretanto esta função pode trabalhar com todas datas anteriores à, porém usá-la desta maneira não tem muito sentido. O Calendário Gregoriano não foi instituído até 15 de Outubro de 1582 (ou 5 de Outubro de 1582, no calendário JDC ("Julian Day Count")). Alguns países aceitaram este calendário um pouco mais tarde. Por exemplo, a Grã Bretanha converteu-se em 1752, a URSS em 1918 e a Grécia em 1923. A maioria dos países Europeus usaram o calendário "Julian" (JDC) antes do calendário Gregoriano.
Parâmetros
- month
-
O mês como um número entre 1 (para janeiro) e 12 (para dezembro)
- day
-
O dia como um número entre 1 e 31
- year
-
O ano como um número entre -4714 e 9999
Valor Retornado
A data juliana para a dada data gregoriana como um inteiro.
Exemplos
Exemplo #1 Funções de Calendário
<?php
$jd = GregorianToJD (10,11,1970);
echo "$jd\n";
$gregorian = JDToGregorian ($jd);
echo "$gregorian\n";
?>
Veja Também
- jdtogregorian() - Converte uma data no formato "Julian Day Count" para o formato Gregoriano
- cal_to_jd() - Converte um calendário (suportado) para o calendário "Julian Day Count"
If you need the same output as the g_date_get_julian function of the GlibC, here is my php implementation :
<?php
/**
* Glib g_date_get_julian PHP implementation
*
* @param $str Date string in a format accepted by strtotime
* @author jfg
*/
private function _get_julian( $str )
{
$d = date_create($str);
if( $d == false )
return 0;
$day_in_year = (int) date_format($d, "z");
$year = (int) date_format($d, "Y") - 1;
$julian_days = $year * 365;
$julian_days += ($year >>= 2);
$julian_days -= ($year /= 25);
$julian_days += $year >> 2;
$julian_days += $day_in_year + 1;
return ceil($julian_days);
}
?>
<?php
/*
* ComputeDateDifference(...)
* Description:
* Calculates the difference between two dates.
*
* Parameter:
* $m0, $d0, $y0 => 1. Moth/Day/Year
* $m1, $d1, $y1 => 2. Moth/Day/Year
*
* Return:
* Difference between given dates in days.
*
* Autor:
* 06.06.2006 - Christian Meyer <ryker@ridgex.net>
*/
function ComputeDateDifference($m0,$d0,$y0,$m1,$d1,$y1)
{
$x0 = gregoriantojd($m0,$d0,$y0);
$x1 = gregoriantojd($m1,$d1,$y1);
$diff = $x1 - $x0;
if ($diff < 0)
$diff *= -1; // abs
return $diff;
}
?>
You can obtain the decimal fraction of the Julian date with the php gregoriantojd() function or the function shown below by applying this code to the returned value.
<?php
$julianDate = gregoriantojd($month, $day, $year);
//correct for half-day offset
$dayfrac = date('G') / 24 - .5;
if ($dayfrac < 0) $dayfrac += 1;
//now set the fraction of a day
$frac = $dayfrac + (date('i') + date('s') / 60) / 60 / 24;
$julianDate = $julianDate + $frac;
?>
This function also ignores decimal fractions in JD dates, and it uses non-standard format for returning the Gregorian date.
So, if your JD date is 2453056.28673, the Gregorian returned value is 2/20/2004, not "2004-02-20 23:45:36"
The decimal part is important, since the Julian day begins at noon, for example 2453056.49 is on Friday, 2453056.50 is on Saturday. Discarding the decimal part means that your returned Gregorian Date will be wrong 50% of the time.
