I recently found out that the DateTime library in C# provides the ability for much higher resolution of time compared to the PHP date time function. While PHP can go down to an impressive microsecond, C# can go all the way down to nanoseconds. Though I am still skeptical that it can actually calculate that small of a division of a second, it does return an impressively large representative number. Since PHP does not go down to nanoseconds it requires a bit of extra effort to convert between nanoseconds and seconds, but it is definitely doable and even fairly trivial once you know the requirements.
Some background on the PHP side of things for those who may not know, when dealing with seconds in PHP it comes from the Unix epoch and is called a Unix timestamp. The Unix epoch date is 01-01-1970 (some have a slightly different figure, but you can find that debate yourself). So 00000000 is 01-01-1970 when converted from the Unix timestamp to something human readable. 05-15-1986 is 51552440.
Taking that knowledge we need to determine when the Unix epoch occurs in DateTime.Ticks. Though I was working with Ticks generated from C# I did not have access to C# while writing this, but luckily Kramer Campbell provided the answer for us in an article on his website. You can read his article for more information on finding this number.
Unix epoch date in C# DateTime.Ticks
621355968000000000
Converting DateTime.Ticks to a Unix timestamp in PHP
Now that we know the Unix epoch in Ticks we can use a simple equation to do the conversion for us. First we need to subtract the current Tick value from the Unix epoch Tick value (Remember, Unix timestamps start at the Unix epoch), then we need to convert the result from nanoseconds into seconds.
Equation
(TICKS – 621355968000000000) / 10000000
The PHP function to do this is as follows
function ticks_to_time($ticks) {
return ($ticks – 621355968000000000) / 10000000;
}
Simple enough right? What if we are using a service where not only do we need to be able to read the DateTime.Ticks, but also we need to convert into Ticks as well.
Converting a Unix timestamp to DateTime.Ticks
We already have everything we need. Simple reverse the equation from above and you will get the nanoseconds from seconds.
Equation
(UNIX_TIMESTAMP * 10000000) + 621355968000000000
Not to hard right? The problem you will face lies with PHPs automatic conversion of this large number into scientific notation. An annoyance, but not really a big deal if you know how to deal with it. We run another mathematical equation on it, or even loop through and change each number in sequence manually but there is no need for that. PHP provides us with a very usable solution in the form of number_format().
Here is what you need to know for number_format() to work.
number_format(UNIX_TIMESTAMP, 0, ‘.’, ‘’);
Putting that all together in a simple function results in
function time_to_ticks($time) {
return number_format(($time * 10000000) + 621355968000000000 , 0, '.', '');
}
Converting strings into DateTime.Ticks
Now that I had the ability to seamlessly convert between these two systems I also needed to find the Ticks N X ago, E.G. 10 Minutes ago. PHP provides the strtotime() function which let’s us find the Unix timestamp in plain English. For example, to find the Unix timestamp from 10 minutes ago you would use
strtotime(“-10 minutes”);
Pretty simple; instead of running the necessary functions manually in PHP I created another function to handle it for me. This function reuses the other functions we defined above and will take in and valid string PHP can convert into a Unix timestamp and convert it into DateTime.Ticks.
Here is the code:
function str_to_ticks($str) {
return time_to_ticks(strtotime($str));
}
Wrap it up!
There you go, now you have the knowledge to convert between C# DateTime.Ticks and Unix timestamps in PHP. Take the provided code and toss it into a file somewhere which you can easily reference, or expand and build it into your own library. Happy timing!
Download PHP code to convert DateTime.Ticks to a Unix timestamp and back from github