Convert Windows Timestamp to Date Using PHP on a Linux Box

Convert Windows Timestamp to date using PHP on a Linux Box

According to this, the windows timestamp you have there is the number of 100-ns since Jan 1st 1601. Therefore, you could just convert it to a unix timestamp using the following formula:

tUnix = tWindow/(10*1000*1000)-11644473600;

You divide by 10*1000*1000 to convert to seconds since Jan 1st 1601 and then you discount 11644473600 which is the number of seconds between Jan 1601 and Jan 1970 (unix time).

So in PHP:

date("d-m-Y H:i:s", $lastlogontimestamp/10000000-11644473600);

EDIT: Interestingly, I got a different offset than Baba. I got mine with Java:

Calendar date1 = Calendar.getInstance(); date1.set(1601, 1, 1);
Calendar date2 = Calendar.getInstance(); date2.set(1970, 1, 1);
long dt = date2.getTimeInMillis() - date1.getTimeInMillis();
System.out.println(String.format("%f", dt / 1000.0)); // prints "11644473600.000000"

According to this SO: Ways to Convert Unix/Linux time to Windows time my offset is correct.

PHP : How to convert a Windows FILETIME String from LDAP into a readable DATE?

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).

How to convert timestamps to dates in Bash?

On systems with GNU Coreutils >= 5.3.0, e.g. Linux you can use:

date -d @1267619929

Convert timestamp from joulemeter to human readable form

after whole day of research, finally i'm able figure the solution.
The timestamp format mentioned is not an UNIX timestamp which start from
00:00:00 1/1/1970 and currently using as start date on PHP date() function.
It's actually start from 0001-01-01 00:00:00.

number of second between 0001-01-01 00:00:00 and 00:00:00 1/1/1970 = 62136892800

Solution:

//timestamp in millisecond
$timestamp = 63573171824975/1000 - 62136892800;
echo date('r',$timeStamp);

//result = Tue, 7 Jul 2015 03:49:35 +0800

PHP date calculation

If you do not want or you cannot use Zend Framework or Pear package try this function i hope this would help:

function dateDifference($date1, $date2)
{
$d1 = (is_string($date1) ? strtotime($date1) : $date1);
$d2 = (is_string($date2) ? strtotime($date2) : $date2);

$diff_secs = abs($d1 - $d2);
$base_year = min(date("Y", $d1), date("Y", $d2));

$diff = mktime(0, 0, $diff_secs, 1, 1, $base_year);

return array
(
"years" => abs(substr(date('Ymd', $d1) - date('Ymd', $d2), 0, -4)),
"months_total" => (date("Y", $diff) - $base_year) * 12 + date("n", $diff) - 1,
"months" => date("n", $diff) - 1,
"days_total" => floor($diff_secs / (3600 * 24)),
"days" => date("j", $diff) - 1,
"hours_total" => floor($diff_secs / 3600),
"hours" => date("G", $diff),
"minutes_total" => floor($diff_secs / 60),
"minutes" => (int) date("i", $diff),
"seconds_total" => $diff_secs,
"seconds" => (int) date("s", $diff)
);
}

How to get date and time from server

You should set the timezone to the one of the timezones you want.

// set default timezone
date_default_timezone_set('America/Chicago'); // CDT

$info = getdate();
$date = $info['mday'];
$month = $info['mon'];
$year = $info['year'];
$hour = $info['hours'];
$min = $info['minutes'];
$sec = $info['seconds'];

$current_date = "$date/$month/$year == $hour:$min:$sec";

Or a much shorter version:

// set default timezone
date_default_timezone_set('America/Chicago'); // CDT

$current_date = date('d/m/Y == H:i:s');

How to convert LDAP timestamp to Unix timestamp

Please see here.

Actually it boils down to converting the FILETIME timestamp into a UNIX timestamp:

$fileTime = "130435290000000000";
$winSecs = (int)($fileTime / 10000000); // divide by 10 000 000 to get seconds
$unixTimestamp = ($winSecs - 11644473600); // 1.1.1600 -> 1.1.1970 difference in seconds
echo date(DateTime::RFC822, $unixTimestamp);

Ways to Convert Unix/Linux time to Windows time

This is described in Converting a time_t Value to a File Time, which provides sample C code.

Summary:

filetime = (unixtime * 10000000) + 116444736000000000

0x3DE43B0C → 0x01C295C4:91150E00 (2002-11-27 03:25:00 +0000)

(Also helpful is How to recognize different types of timestamps from quite a long way away.)


Often it is easier to parse the human-readable timestamp, such as "2002-11-27 03:25:00" (printf %AF %AT), using strptime() or similar.



Related Topics



Leave a reply



Submit