当前位置: 移动技术网 > IT编程>开发语言>PHP > php中time()与$_SERVER[REQUEST_TIME]用法区别

php中time()与$_SERVER[REQUEST_TIME]用法区别

2019年06月02日  | 移动技术网IT编程  | 我要评论

本文实例详细讲述了php中time()与$_server[request_time]用法的区别。分享给大家供大家参考。具体分析如下:

简单的说time()与$_server["request_time"]都是获得时间的,但time返回当前的 unix 时间戳而$_server["request_time"]得到请求开始时的时间戳,稍有区别。
1. time() 获取当前的系统时间戳

int time(void) :

返回当前的 unix 时间戳 (返回自从 unix 纪元(格林威治时间 1970 年 1 月 1 日 00:00:00)到当前时间的秒数。 )

自 php 5.1 起在 $_server['request_time'] 中保存了发起该请求时刻的时间戳。

$_server["request_time"] :the timestamp of the start of the request. available since php 5.1.0. 一看解释就都明白了

2. $_server["request_time"] 得到请求开始时的时间戳

实例代码:

复制代码 代码如下:
<?php
    date_default_timezone_set('prc');
    sleep(5);//php脚本睡5秒
    echo date('y-m-d h:i:s',time());//获取当前系统时间的时间戳
    echo '<hr />';
    echo date('y-m-d h:i:s',$_server['request_time']);//得到请求此php脚本时的时间戳
?>

输出结果截图如下:

结论:

因为sleep(5)睡了5秒,之后用time()获取系统的时间戳,$_server['request_time']则记录了发起此请求时刻的时间戳。所以$_server['request_time']比用time()获取的时间戳早5秒

希望本文所述对大家的php程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网