Skip to content

Instantly share code, notes, and snippets.

@caseydunham
Created June 30, 2015 18:53
Show Gist options
  • Save caseydunham/508e2994e1195e4cb8e4 to your computer and use it in GitHub Desktop.
Save caseydunham/508e2994e1195e4cb8e4 to your computer and use it in GitHub Desktop.
Convert Active Directory TimeStamps to and from Unix Timestamps
// based on http://stackoverflow.com/questions/15770879/unix-timestamp-to-ldap-timestamp
<?php
date_default_timezone_set('America/New_York');
// Microsoft TIMESTAMP epoch is Jan 1, 1601 http://www.selfadsi.org/deep-inside/microsoft-integer8-attributes.htm
// More Info:
// http://blogs.technet.com/b/askds/archive/2009/04/15/the-lastlogontimestamp-attribute-what-it-was-designed-for-and-how-it-works.aspx
function ldapTimeToUnixTime($ldapTime) {
$secsAfterADEpoch = $ldapTime / 10000000;
$ADToUnixConverter = ((1970 - 1601) * 365 - 3 + round((1970 - 1601) / 4)) * 86400;
return intval($secsAfterADEpoch - $ADToUnixConverter);
}
function unixTimeToLdapTime($unixTime) {
$ADToUnixConverter = ((1970 - 1601) * 365 - 3 + round((1970 - 1601) / 4)) * 86400;
$secsAfterADEpoch = intval($ADToUnixConverter + $unixTime);
return $secsAfterADEpoch * 10000000;
}
$current_unix_ts = time();
print "Current time from unix time: " . date("Y-m-d\TH:i:s\Z", $current_unix_ts) . "\n";
$afteradconv = unixTimeToLdapTime($current_unix_ts);
print "After AD Conversion: " . $afteradconv . "\n";
$ldapconv = ldapTimeToUnixTime($afteradconv);
print "After AD to Unix conversion: " . date("Y-m-d\TH:i:s\Z", $ldapconv) . "\n";
@mendel129
Copy link

awesome! thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment