<?php
// original script:
// http://www.codeproject.com/datetime/exceldmy.asp
// Excel/Lotus 123 have a bug with 29-02-1900.
// 1900 is not a leap year, but Excel/Lotus 123 think it is...
function excel_D2DMY($days) {
if ($days <1) return "";
if ($days == 60) {
return array('day'=>29,'month'=>2,'year'=>1900);
} else {
if ($days < 60) {
// Because of the 29-02-1900 bug, any serial date
// under 60 is one off... Compensate.
++$days;
}
// Modified Julian to DMY calculation with an addition of 2415019
$l = $days + 68569 + 2415019;
$n = floor(( 4 * $l ) / 146097);
$l = $l - floor(( 146097 * $n + 3 ) / 4);
$i = floor(( 4000 * ( $l + 1 ) ) / 1461001);
$l = $l - floor(( 1461 * $i ) / 4) + 31;
$j = floor(( 80 * $l ) / 2447);
$nDay = $l - floor(( 2447 * $j ) / 80);
$l = floor($j / 11);
$nMonth = $j + 2 - ( 12 * $l );
$nYear = 100 * ( $n - 49 ) + $i + $l;
$ret = array('day'=>$nDay,'month'=>$nMonth,'year'=>$nYear);
return $ret;
}
}
/**
* @desc Returns Excel serialDate constant calculated from gregorian date
* original javascript found @ http://wwwmacho.mcmaster.ca/JAVA/JD.html
* ported to PHP by XL:-)
*
* tested compatibility with EXCEL dates between 01/01/1900..31.12.2099,
* year given eventually as 0 (=1900)..199 (=2099)
* adds also days if supplied argument more than real number of days
* in particular month (same applies to months)
* as for years bellow 1900: algorhytm is not exact, therefore limitation
* for years 1900 - 2099
*/
function excel_DMY2D($d, $m, $y, $uh=0, $um=0, $us=0) {
if($y<1900){
if($y<0 || $y>199){
echo ' !!! Invalid year ['.$y.'], must be between 1900 - 2099 or 0 - 199 !!!';
return false;
}else{
$y += 1900;
}
}
$extra = 100.0*$y + $m - 190002.5;
$rjd = 367.0*$y;
$rjd -= floor(7.0*($y+floor(($m+9.0)/12.0))/4.0);
$rjd += floor(275.0*$m/9.0);
$rjd += $d;
$rjd += ($uh + ($um + $us/60.0)/60.)/24.0;
$rjd += 1721013.5;
$rjd -= 0.5*$extra/abs($extra);
$rjd += 0.5;
$rjd -= 2415020.5; // JD correction constant for 01/01/1900
$rjd += ($rjd>60) ? 2 : 1; // adjust to inheritet EXCEL/LOTUS bug
return $rjd;
}
?>
JDToGregorian
(PHP 4, PHP 5)
JDToGregorian — Converts Julian Day Count to Gregorian date
Description
string jdtogregorian
( int $julianday
)
Converts Julian Day Count to a string containing the Gregorian date in the format of "month/day/year".
Parameters
- julianday
-
A julian day number as integer
Return Values
The gregorian date as a string in the form "month/day/year"
JDToGregorian
lubosdz at hotmail dot com
09-Aug-2007 01:10
09-Aug-2007 01:10
ion at gluch dot org dot mx
13-Dec-2006 07:08
13-Dec-2006 07:08
I have used the pctips function to convert an Excel date, just add 2415018.5 (excel epoch) to the numeric date.
// Excel shows 38856 for 19/05/2006
print jd_to_greg(38856 + 2415018.5); // Prints 19/05/2006.
pctips
07-Jul-2005 12:12
07-Jul-2005 12:12
I have changed treebe's function a bit:
<?php
function jd_to_greg($julian) {
$julian = $julian - 1721119;
$calc1 = 4 * $julian - 1;
$year = floor($calc1 / 146097);
$julian = floor($calc1 - 146097 * $year);
$day = floor($julian / 4);
$calc2 = 4 * $day + 3;
$julian = floor($calc2 / 1461);
$day = $calc2 - 1461 * $julian;
$day = floor(($day + 4) / 4);
$calc3 = 5 * $day - 3;
$month = floor($calc3 / 153);
$day = $calc3 - 153 * $month;
$day = floor(($day + 5) / 5);
$year = 100 * $year + $julian;
if ($month < 10) {
$month = $month + 3;
}
else {
$month = $month - 9;
$year = $year + 1;
}
if ($day < 10) {
$day = "0".$day;
}
if ($month < 10) {
$month = "0".$month;
}
return $day."/".$month."/".$year;
}
?>
So, if the in the old format the 1st January 2005 was "1.1.2005", now the function will return "01/01/2005". I hope it could be useful to someone :)
httpwebwitch
22-Jun-2004 08:37
22-Jun-2004 08:37
JD days may have decimal fractions which correspond to the time of day. The Julian day begins at noon, and the decimal fraction measures fractional days until the beginning of the next day at noon.
For instance, Julian Day 2453179.00000 is June 22, 2004, at 12:00pm (noon).
One hour later, it's 2453179.04167
At 2453179.20833 I'll have dinner, and
at 2453179.45833, it's time for the evening news.
After a good night of sleep, my alarm will go off at 2453179.83333,
then at noon on June 23, a new Julian Day begins at 2453180.
To use these functions with fractional days, strip the fractional part with floor(), and apply the function to the integer part.
Then add 12 hours, bringing you to noon of that day. That is the actual time returned by JDToGregorian().
Then add the fractional part of the day, by multiplying the decimal part of the Julian Day by (24*60*60) seconds. This may take you forward or backward to a different Gregorian calendar date.
uni_fl4r3 at t hotmail dot com
14-Sep-2003 06:05
14-Sep-2003 06:05
I have made a slight modification to treebe's jd to greg function, this one will transform a unix timestamp to Gregorian day/month/year format...
<?php
function unix_to_greg($unix_timestamp) {
$julian = floor(((($unix_timestamp / "60") / "60") / "24") + "2440588");
$julian = $julian - 1721119;
$calc1 = 4 * $julian - 1;
$year = floor($calc1 / 146097);
$julian = floor($calc1 - 146097 * $year);
$day = floor($julian / 4);
$calc2 = 4 * $day + 3;
$julian = floor($calc2 / 1461);
$day = $calc2 - 1461 * $julian;
$day = floor(($day + 4) / 4);
$calc3 = 5 * $day - 3;
$month = floor($calc3 / 153);
$day = $calc3 - 153 * $month;
$day = floor(($day + 5) / 5);
$year = 100 * $year + $julian;
if ($month < 10)
{
$month = $month + 3;
}else{
$month = $month - 9;
$year = $year + 1;
}
return "$day.$month.$year";
}
?>
treebe
31-May-2003 03:01
31-May-2003 03:01
Julian to Gregorian date change.
If you do not have the calendar extensions loaded this is little function works realy well.
<?php
function jd_to_greg($julian) {
$julian = $julian - 1721119;
$calc1 = 4 * $julian - 1;
$year = floor($calc1 / 146097);
$julian = floor($calc1 - 146097 * $year);
$day = floor($julian / 4);
$calc2 = 4 * $day + 3;
$julian = floor($calc2 / 1461);
$day = $calc2 - 1461 * $julian;
$day = floor(($day + 4) / 4);
$calc3 = 5 * $day - 3;
$month = floor($calc3 / 153);
$day = $calc3 - 153 * $month;
$day = floor(($day + 5) / 5);
$year = 100 * $year + $julian;
if ($month < 10) {
$month = $month + 3;
}
else {
$month = $month - 9;
$year = $year + 1;
}
return "$day.$month.$year";
}
?>
