Link to home
Start Free TrialLog in
Avatar of jbreg
jbreg

asked on

Convert Delphi Formatted Date Time to regular DateTime via PHP

Hi,

I am converting a script written in ASP to PHP. It reads a file created in Delphi. One particular function converts the Delphi-formatted times (e.g. 38837.0129173727) to a string of normal date time format (DD/MM/YYYY HH:MM:SS).

Here is the script

function ConvertDateTime(DelphiDateTime,DateTime)
      Days = Int(DelphiDateTime)
       Fraction = DelphiDateTime - Days
      Seconds = Fraction / (1/24/60/60)
      DateTime = CStr(DateAdd("d",Days,"30/12/1899 00:00:00"))
      DateTime = CStr(DateAdd("s",Seconds,DateTime))      
End Function

I am trying to convert this to PHP but really have no idea how delphi times are formatted in the first place. This function seems odd because the two arguments are exactly the same and are both the same delphi datetime values to be converted.

I've converted to PHP as

function ConvertDateTime($DelphiTime,$DateTime) {
      $Days = intval($DelphiTime);
      $Fraction = $DelphiTime - $Days;
      $Seconds = $Fraction / (1/24/60/60);
      $DateTime = strval(DateAdd("d",$Days,"30/12/1899 00:00:00"));
      $DateTime = strval(DateAdd("s",$Seconds,$DateTime));
      return $DateTime;
}

But when I pass echo ConvertDateTime(38837.0129173727,38837.0129173727) I get

Warning: mktime(): Windows does not support negative values for this function in ...

I am using a standard DateAdd function in PHP

function DateAdd($interval, $number, $date) {

    $date_time_array = getdate($date);
    $hours = $date_time_array['hours'];
    $minutes = $date_time_array['minutes'];
    $seconds = $date_time_array['seconds'];
    $month = $date_time_array['mon'];
    $day = $date_time_array['mday'];
    $year = $date_time_array['year'];

    switch ($interval) {
   
        case 'yyyy':
            $year+=$number;
            break;
        case 'q':
            $year+=($number*3);
            break;
        case 'm':
            $month+=$number;
            break;
        case 'y':
        case 'd':
        case 'w':
            $day+=$number;
            break;
        case 'ww':
            $day+=($number*7);
            break;
        case 'h':
            $hours+=$number;
            break;
        case 'n':
            $minutes+=$number;
            break;
        case 's':
            $seconds+=$number;
            break;            
    }
       $timestamp= mktime($hours,$minutes,$seconds,$month,$day,$year);
    return $timestamp;
}

How are these delphi times actually formatted and how might I use this knowledge to develop a script to convert them using PHP?


Avatar of dr_dedo
dr_dedo
Flag of Egypt image

how is date time calculate in delphi ?? what date/time does 38837.0129173727 represents ??
Avatar of jbreg
jbreg

ASKER

No idea. That's part of the question, I will ask in the Delphi Area.
Avatar of Eddie Shipman
From the Delphi help file:
"The integral part of a Delphi TDateTime value is the number of days that have
passed since 12/30/1899. The fractional part of the TDateTime value is fraction
of a 24 hour day that has elapsed."

The value 38837.0129173727  = 30-04-2006 00:18:36
Have you echo'ed the variables in your DateAdd function?
Seems to be returning incorrect results.

DelphiTime:38837.0129174
Days:38837
Fraction:0.0129173726964
Seconds:1116.06100097
Hours:19
Mins:0
Secs:30
Mon:12
Day:364
Year:1969
DateTime:-61
Hours:18
Mins:58
Secs:59
Mon:12
Day:364
Year:1969
DateTime:-1


How the question really should read is:
"I have a floating point variable that corresponds to a timestamp where the integral part
is the number of days that have passed since 12/30/1899 and the fractional part is
fraction of a 24 hour day that has elapsed. How do I convert this to a PHP timestamp?"

Remember, you can subtract 25569 to make the value begin at 1/1/1970 for PHP timestamp
values.

all credit goes to the delphi hero :)

this code would print 30-04-2006 0:18:36
<?
$d = 38837.0129173727;
$days = intval($d)-25568;
$fraction = ''+38837.0129173727;
$fraction = substr($fraction,strpos($fraction,'.'));
$sec = $fraction * 24*60*60;
$date = date('d-m-Y G:i:s',mktime(0,0,$sec,1,$days,1970));
echo $date;
?>
ASKER CERTIFIED SOLUTION
Avatar of dr_dedo
dr_dedo
Flag of Egypt image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think it's cool how mktime automatically added the number of days to 1970.
guess point split with EddieShipman is fair
wish questioner didn't abandon that question !
OK, but give dr_dedo 2/3 as he is the one that came up with the algorithm. I only provided the info
on how Delphi DateTime is designed.
I'm not sure if we can assign unequal shares of points in cleanup process, but I'll discuss this with the cleanup admin and let you know.
The OP could split them for you if he's listening.
I verified what I thought about splitting the points with cleanup admin, and yes, the shares going to experts are always equal. We cannot assign unequal points to experts involved, while the asker can.
I will leave the final decision to be made by a moderator.
Regards,
Huji
equal points are OK !!
EddieShipman didn't do less than me here !!!!