Skip to content

Instantly share code, notes, and snippets.

@vensires
Created November 3, 2016 15:28
Show Gist options
  • Save vensires/34ca52a56262290d61fb56e9ce0a330d to your computer and use it in GitHub Desktop.
Save vensires/34ca52a56262290d61fb56e9ce0a330d to your computer and use it in GitHub Desktop.
PHP function to turn a DateTime object to Delphi's TDate type. Delphi's TDate type is something like UnixTimestamp; it is a (float) number but it has as a starting poing the 1899-12-30 instead of the 1970-01-01 as is the case in the UnixTimestamp.
<?php
/**
* For the sake of example, I assume that the caller only needs the date without any time information.
* If you also need the time, you will have to modify the following code a little.
* @param object $phpDate
* A DateTime PHP object
* @return int
* The number of days from 1899-12-30 (Delphi's starting point). This is also the correct value to be
* stored as a TDate type variable in Delphi.
*/
function php2delphiTDate(DateTime $phpDate) {
// The difference between UnixTimestamp's starting point and DelphiTDate's starting point.
$UnixDelphiDiff = 25569;
// Create a DateTime object based on the starting point of the Unix Timestamp.
$unixDate = date_create_from_format('d/m/Y H:i:s', '01/01/1970 00:00:00');
// Get the difference in a DateInterval object.
$interval = $phpDate->diff($unixDate);
// Sum the days of the difference with the days of the difference between
// UnixTimestamp first date and Delphi's first date.
return $interval->days + $UnixDelphiDiff;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment