0

I'm querying MS LDAP and some date fields about user have this kind of value : 132497313049180481.

It seems to be in the Windows FILETIME format that is use for some informations in LDAP and Active Directory as a timestamp.

How to convert it into a readable DATE ?

Velome
  • 158
  • 1
  • 1
  • 15

2 Answers2

1

This convert Windows FILETIME (ex: 132497313049180481) to a human readable datetime (in this case: "2020-11-13 08:55:04").

I hope it can be usefull :

$filetime = "132497313049180481";
echo filetimeToStr($filetime); // Will display "2020-11-13 08:55:04"

function filetimeToStr($filetime){
  date_default_timezone_set ("UTC"); //For a result not depending on server time zone.
  $resp = (int)($filetime / 10000000); //Number of seconds since 1601-01-01.
  $diff = 11644473600; //Number of seconds between FILETIME & Unix timestamp.
  $resp = $resp - $diff; //Actual Unix timestamp matching your filetime.
  $resp = date("Y-m-d H:i:s", $resp);
  return $resp;
}

Wanna know where the 11644473600 come from ?

MSDN say that FILETIME :

Contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC). https://learn.microsoft.com/fr-be/windows/win32/api/minwinbase/ns-minwinbase-filetime?redirectedfrom=MSDN

About Unix timestamp :

Unix epoch is the time 00:00:00 UTC on 1 January 1970 ... every day is treated as if it contains exactly 86400 seconds. https://en.wikipedia.org/wiki/Unix_time

date_default_timezone_set ("UTC"); //For a result not depending on server time zone.
$start_date = date_create("1601-01-01");
$end_date = date_create("1970-01-01");
$diff = date_diff($start_date, $end_date);
$diff = (int)$diff->format("%a"); //Number of days between FILETIME & Unix timestamp
$diff = ($diff * 86400); //Number of seconds between FILETIME & Unix timestamp
echo $diff; //Will display 11644473600

EDITED

Added date_default_timezone_set ("UTC"); for a result not depending on server time zone (Thanks @jspit).

Velome
  • 158
  • 1
  • 1
  • 15
  • Have you just reposted your answer from https://stackoverflow.com/a/64818470/1213708? – Nigel Ren Nov 19 '20 at 08:35
  • Hello, yes, the title of the question mentioned C# and question was about php. Also the accepted answer was a simple link with no code solution. I didn't find a complete solution to my described problem on the web so I wanted to add a Q&A here with the exact problem I encounter (more presice then the previous one) so it help other people. I'm new here I hope I didn't do wrong. Have a nice day. – Velome Nov 19 '20 at 08:40
  • 1
    For a result "2020-11-13 08:55:04" must a date_default_timezone_set ('UTC'); to be added. If the server is set to a different time zone, the result of your function will be different. I like that your solution is also explained in detail. Rated your answer useful. – jspit Nov 20 '20 at 07:35
  • Thanks A LOT @jspit , I edited my answer to add this precious part. – Velome Nov 20 '20 at 11:33
  • Please close new duplicates and add new/valuable/unique insights to old answers so that researchers don't have to compare multiple pages to compare answers. – mickmackusa Nov 20 '20 at 12:17
0

The createDateTimeFromSystemTime() function returns a DateTime object. It can be used universally and also works under 32-bit systems. With other parameters for $basis and $resolution, this function can also process other time stamps (LabVIEW Timestamp, Mac Timestamp ..)

function createDateTimeFromSystemTime(
  $time,  //num.String, integer or float
  $basis = '1601-1-1',
  $resolution = 1.E-7,
  $timeZone = 'UTC'
){
  return date_create($basis.' UTC')
    ->modify(round($time * $resolution).' Seconds')
    ->setTimeZone(new DateTimeZone($timeZone));
}

How to use for LDAP-Timestamp:

$ldapTimestamp = "132497313049180481";

$dateTime = createDateTimeFromSystemTime($ldapTimestamp);

echo $dateTime->format('Y-m-d H:i:s T');
//2020-11-13 08:55:05 UTC

The algorithm is from this class.

jspit
  • 7,276
  • 1
  • 9
  • 17
  • Please close new duplicates and add new/valuable/unique insights to old answers so that researchers don't have to compare multiple pages to compare answers. – mickmackusa Nov 20 '20 at 12:17