PHP Equivalent Ticks C #

I am currently converting an ASP.NET C # application to PHP. The service uses DateTime Ticks , and I was wondering if there is an equivalent in PHP.

If not the best way for me to calculate the time span?

+4
source share
5 answers

Use microtime(true)

Pay attention to the following, directly from the PHP manual

By default, microtime () returns a string in the form msec sec, where sec is the current time, measured in seconds since the Unix epoch (0:00:00 AM January 1, 1970 GMT), and msec is the number of microseconds elapsed from seconds, expressed in seconds.

If get_as_float is set to TRUE, then microtime () returns a float that represents the current time in seconds since Unix, accurate to the nearest microsecond.

+4
source

[Edit: I saw later that the DateTime ticks are also very similar to the PHP DateTime-> diff method, so I added it; realized that I missed the parameter for microtime, thanks to freedom of speech.]

I usually do this for benchmarking (kudo also for freedom of speech):

 $time = microtime(true); // do your thing $diff = microtime(true) - $time; 

See: http://php.net/microtime

On other dates, you can do it like this:

 $date1 = new DateTime('05-04-2010'); $date2= new DateTime('yesterday'); // returns a DateInterval object $diff = $date1->diff($date2); 

The DateInterval object has the following topology:

  public integer $ DateInterval-> y;
 public integer $ m;
 public integer $ d;
 public integer $ h;
 public integer $ i;
 public integer $ s;
 public integer $ invert;
 public mixed $ days;

DateTime Class http://www.php.net/manual/en/class.datetime.php

Diff Method http://www.php.net/manual/en/datetime.diff.php

DateInterval Object http://www.php.net/manual/en/class.dateinterval.php

+2
source

Here's how you do it ... Looks like me like a normal problem. The big "magic number" is the difference between 1970-1-1 and 1-1-1.


 $mtime = microtime(false); list($usec, $sec) = explode(" ", $mtime); $usec = (string)($usec * 10000000); $timestamp = gmp_add(gmp_add(gmp_mul($sec, "10000000"), (string)$usec), "621355968000000000"); 
+2
source

I think microtime is what you want

0
source

If you don't want to recompile PHP using gmp, this works:

 $time=time() number_format(($time * 10000000) + 621355968000000000 , 0, '.', ''); 

Use microtime to get usec, which can then overwrite the trailing 0s.

0
source

All Articles