Convert ticks to unix timestamp
Asked Answered
S

4

7

I m getting start time 634942626000000000 and end time 634942638000000000. How can I convert it to a Unix timestamp? I want to display formatted date/time in PHP?

Sukhum answered 20/1, 2013 at 18:2 Comment(0)
F
10

C# ticks is a number of ticks since midnight 0001-01-01 00:00:00 (every tick is 1/10000000 of second) and UNIX timestamp is number of seconds since beginning of the UNIX epoch (1970-01-01 01:00:00), so desired result is $seconds = 634942626000000000/10000000 - $number_of_seconds, where $number_of_seconds is seconds between 0001-00-01 00:00:00 and 1970-01-01 01:00:00. So, all you need now is to determine what $number_of_seconds is equals.

Update: this is how to do it in C# : How to convert a Unix timestamp to DateTime and vice versa?

Update 2 Using this simple script http://hastebin.com/lefiyiheju.mel determined that $number_of_seconds is 62136892800, but I don't know if it is much accurate

Finger answered 20/1, 2013 at 18:42 Comment(7)
6.214×10^10 seconds is the result wolfram alpha gives. for number of seconds between 0001 and 1970 however that is not accurate enough for the kind of calculation he is looking for.Steinberg
May be this can be done using C# + bigint, because it calculates time from 0001-01-01 00:00:00 everywhere, but I don't know C# languageFinger
Thanks a lot for detail reply..I really appreciate it.Sukhum
I've calculated $number_of_seconds = 62135600400. And this seems to be accurate up to seconds, which is good enough for what I want to use it for.Dissonancy
Math.floor(numOfTicks/10000 - 62136892800000) // JS timestamp (ms) new Date(Math.floor(numOfTicks/10000 - 62136892800000)) // js DateStipitate
Isn't your UNIX epoch off by 1 hour? from what i found, Unix Timestamp 0 is equal to: 621355968000000000 Ticks. Shouldn't $num_of_secs be 621355968000000000/10000000 ?Breechblock
@Breechblock that is correct. DateTime.UnixEpoch.Ticks == 621355968000000000.Cracow
K
1

This should do it:

$seconds = 634942626000000000 / 1000000000;
echo date("Y-m-d H:i:s", $seconds);
Karinakarine answered 20/1, 2013 at 18:5 Comment(3)
I tried but returns 1969-12-31 19:32:49. it must be current today date/time. I m getting it from 122.155.1.10/program-info/pginfo-20130120.txtSukhum
Yes, I already fixed it. still i get 1969-12-31 19:32:49 . Any suggestion?Sukhum
Ticks count from 0001-01-01, Unix time stamps count from 1970-01-01, so this answer is wrong.Cowage
C
0

@Sinkeat had the right answer in the comments for https://mcmap.net/q/1467539/-convert-ticks-to-unix-timestamp.

Console.WriteLine($"{DateTime.UnixEpoch}: {DateTime.UnixEpoch.Ticks}");

prints 1/1/1970 12:00:00 AM: 621355968000000000. So

$number_of_seconds = 62135596800
$seconds = $ticks/10000000 - $number_of_seconds

should do it. I don't have a PHP interpreter up to test this, but a similar formula in Google Sheets does work with the above numbers, e.g.

=EPOCHTODATE(C2/10000000-62135596800)
Cracow answered 26/7 at 23:27 Comment(0)
I
-1

In Python, you'll do

In [53]: import datetime

In [54]: ticks = 634942626000000000

In [55]: start = datetime.datetime(1, 1, 1)

In [56]: delta = datetime.timedelta(seconds=ticks/10000000)

In [57]: the_actual_date = start + delta

In [58]: the_actual_date.timestamp()
Out[58]: 1358665800.0

In [59]: the_actual_date
Out[59]: datetime.datetime(2013, 1, 20, 7, 10)
Irremeable answered 3/10, 2020 at 18:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.