Ricky Watson
Ricky Watson

Reputation: 143

Convert ticks to unix timestamp

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?

Upvotes: 7

Views: 19810

Answers (4)

Bondolin
Bondolin

Reputation: 3121

@Sinkeat had the right answer in the comments for https://stackoverflow.com/a/14427911/1399272.

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)

Upvotes: 1

Lucianovici
Lucianovici

Reputation: 312

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)

Upvotes: -1

Timur
Timur

Reputation: 6718

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

Upvotes: 11

John Conde
John Conde

Reputation: 219934

This should do it:

$seconds = 634942626000000000 / 1000000000;
echo date("Y-m-d H:i:s", $seconds);

Upvotes: 1

Related Questions