Convert ticks to unix timestamp

12,671

Solution 1

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

Solution 2

This should do it:

$seconds = 634942626000000000 / 1000000000;
echo date("Y-m-d H:i:s", $seconds);
Share:
12,671
Ricky Watson
Author by

Ricky Watson

Updated on July 29, 2022

Comments

  • Ricky Watson
    Ricky Watson almost 2 years

    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?

  • Ricky Watson
    Ricky Watson over 11 years
    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.txt
  • Ricky Watson
    Ricky Watson over 11 years
    Yes, I already fixed it. still i get 1969-12-31 19:32:49 . Any suggestion?
  • WhyteWolf
    WhyteWolf over 11 years
    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.
  • Timur
    Timur over 11 years
    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# language
  • Ricky Watson
    Ricky Watson over 11 years
    Thanks a lot for detail reply..I really appreciate it.
  • mozey
    mozey over 8 years
    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.
  • Alireza Mirian
    Alireza Mirian over 6 years
    Math.floor(numOfTicks/10000 - 62136892800000) // JS timestamp (ms) new Date(Math.floor(numOfTicks/10000 - 62136892800000)) // js Date
  • Sinkeat
    Sinkeat about 3 years
    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 ?