?
Solved

Convert Delphi Formatted Date Time to regular DateTime via PHP

Posted on 2006-05-22
17
Medium Priority
?
2,338 Views
Last Modified: 2007-12-19
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?


0
Comment
Question by:jbreg
  • 7
  • 5
  • 2
  • +1
15 Comments
 
LVL 16

Expert Comment

by:dr_dedo
ID: 16738832
how is date time calculate in delphi ?? what date/time does 38837.0129173727 represents ??
0
 

Author Comment

by:jbreg
ID: 16740307
No idea. That's part of the question, I will ask in the Delphi Area.
0
 
LVL 26

Expert Comment

by:Eddie Shipman
ID: 16743492
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:Eddie Shipman
ID: 16744227
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


0
 
LVL 26

Expert Comment

by:Eddie Shipman
ID: 16744288
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.

0
 
LVL 16

Expert Comment

by:dr_dedo
ID: 16744538
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;
?>
0
 
LVL 16

Accepted Solution

by:
dr_dedo earned 1000 total points
ID: 16744576
it would be better if in the form of a function

<?
$d = 38837.0129173727;
echo convert ($d);

//====== convert =========================
function convert ($delphiDate){
  $days = intval($delphiDate)-25568;
  $fraction = ''+$delphiDate;
  $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));
  return $date;
}
//======================================
?>
0
 
LVL 26

Assisted Solution

by:Eddie Shipman
Eddie Shipman earned 1000 total points
ID: 16745577
I was really trying to get that algorithm figured out. couldn't quite get it.
Good work, dr_dedo.

But can you explain why
25569.000001111 = 01/01/1970 00:00:00
and
25569.000000000 = 27-11-1903 17:31:44

0
 
LVL 26

Expert Comment

by:Eddie Shipman
ID: 16745623
I think it's cool how mktime automatically added the number of days to 1970.
0
 
LVL 16

Expert Comment

by:dr_dedo
ID: 16955346
guess point split with EddieShipman is fair
wish questioner didn't abandon that question !
0
 
LVL 26

Expert Comment

by:Eddie Shipman
ID: 16959893
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.
0
 
LVL 14

Expert Comment

by:huji
ID: 16966755
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.
0
 
LVL 26

Expert Comment

by:Eddie Shipman
ID: 16969311
The OP could split them for you if he's listening.
0
 
LVL 14

Expert Comment

by:huji
ID: 16970480
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
0
 
LVL 16

Expert Comment

by:dr_dedo
ID: 16970512
equal points are OK !!
EddieShipman didn't do less than me here !!!!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question