美文网首页
PHP 扩展 - 获取当前时间毫秒

PHP 扩展 - 获取当前时间毫秒

作者: Bun_Wong | 来源:发表于2016-07-05 23:05 被阅读55次

    类似内置函数 microtime 中获取当前时间的毫秒数,下面实现参考了该函数的源代码:

    // azalea.h
    double getMicrotime();
    
    // azalea.c
    #include "ext/date/php_date.h"
    #ifdef PHP_WIN32
    #include "win32/time.h"
    #elif defined(NETWARE)
    #include <sys/timeval.h>
    #include <sys/time.h>
    #else
    #include <sys/time.h>
    #endif
    #define MICRO_IN_SEC 1000000.00
    
    /* {{{ getMicrotime
     */
    double getMicrotime()
    {
        struct timeval tp = {0};
        if (gettimeofday(&tp, NULL)) {
            return 0;
        }
        return (double)(tp.tv_sec + tp.tv_usec / MICRO_IN_SEC);
    }
    /* }}} */
    

    然后调用 getMicrotime 方法即可返回一个 double 类型的当前时间,可用于扩展内计算时间差。

    相关文章

      网友评论

          本文标题:PHP 扩展 - 获取当前时间毫秒

          本文链接:https://www.haomeiwen.com/subject/whfkjttx.html