Created
June 30, 2015 18:53
-
-
Save caseydunham/508e2994e1195e4cb8e4 to your computer and use it in GitHub Desktop.
Convert Active Directory TimeStamps to and from Unix Timestamps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
awesome! thanks!