Get Time Ticks in PHP
Asked Answered
I

3

5

Consider this line of code in C#

ordernumber.Value = DateTime.Now.Ticks.ToString();

How to get same ordernumber.Value in PHP

$ordernumberValue = microtime(); //?

I try to this

echo microtime(true) * 10000000;

But get result string.length was difference. short length than C#.

Insolation answered 13/6, 2015 at 10:23 Comment(2)
Using microseconds for creating order numbers is highly fragile and will not scale. Use a database auto_increment insteadBona
I wonder how many will blindly post silly answers until they realize intelligently, like @Bona did, what the real problem is. Ah that XY problem, omnipresent as always, confusing people with healthy eyes.Slaveholder
B
6

From .NET documentation:

DateTime.Ticks Property

The value of this property represents the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001 (0:00:00 UTC on January 1, 0001, in the Gregorian calendar), which represents DateTime.MinValue. It does not include the number of ticks that are attributable to leap seconds.

In PHP this is implemented simply as time():

time

Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

microtime() similarly returns time in seconds and microseconds after decimal point, so it has greater precision. For some archaic reasons, the default value is a string, but if you pass true as first argument, you'll get a nice float:

rr-@burza:~$ php -r 'echo microtime(true);'
1434193280.3929%    

So all you have to do is to scale the value returned by either time() or microtime() by a constant factor.

According to Wikipedia, a nanosecond is equal to 1000 picoseconds or 1⁄1000 microsecond, or 1/1000000000 second. So 100 nanoseconds would mean 100/1000000000 microseconds, i.e. one .NET tick = 1/10000000 second, i.e. one second = 10000000 .NET ticks. Thus you need to multiply value returned by time() or microtime() by 10000000 like this:

microtime(true) * 10000000
Baldpate answered 13/6, 2015 at 10:55 Comment(2)
Pls note that microtime(true) still only gives seconds with microsecs as decimal. You need to multiply that with 1000 to get microseconds. And then with 10 to get ticks.Romeu
Thanks @Samosa, you're absolutely right. I fixed the maths.Baldpate
R
1

I am not sure if this is what you are looking :-

$mt = microtime(true);

$mt =  $mt*1000; //microsecs
$ticks = (string)$mt*10; //100 Nanosecs
echo $ticks; //14341946614384

Now the major difference is Ticks is 100-Nanoseconds since 12:00:00 midnight, January 1, 0001 while this will produce 100-Nanoseconds since January 1 1970

Romeu answered 13/6, 2015 at 11:8 Comment(1)
15246477093757.0, this was the output for me, still gave me decimal point, how do i get rid of that?Vierra
C
1

One tick is 1/10000000 of second.

This code converts current microtime to "ticks" count:

list($usec, $sec) = explode(" ", microtime());
$ticks = (int)($sec*10000000+$usec*10000000);
Canikin answered 13/6, 2015 at 11:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.