Difference between time () and $ _ SERVER [REQUEST_TIME] in php

Source: Internet
Author: User
This article mainly introduces the differences between time () and $ _ SERVER [REQUEST_TIME] in php, and analyzes in detail the usage of time () and $ _ SERVER [REQUEST_TIME, the differences between the two methods in obtaining the current system timestamp and the request timestamp are compared and summarized in the form of examples. For more information, see the example in this article () it differs from $ _ SERVER [REQUEST_TIME. Share it with you for your reference. The specific analysis is as follows:

In short, both time () and $ _ SERVER ["REQUEST_TIME"] obtain time, however, time returns the current Unix timestamp while $ _ SERVER ["REQUEST_TIME"] returns the timestamp at the start of the request, which is slightly different.
1. time () gets the current system timestamp

Int time (void ):

Returns the current Unix timestamp (returns the number of seconds from the Unix epoch (GMT 00:00:00, January 1, January 1, 1970) to the current time. )

Since PHP 5.1, the timestamp at the TIME when the REQUEST is initiated is saved in $ _ SERVER ['request _ time.

$ _ SERVER ["REQUEST_TIME"]: The timestamp of the start of the request. Available since PHP 5.1.0. you can understand all The explanations at a glance.

2. $ _ SERVER ["REQUEST_TIME"] get the request start timestamp

Instance code:

The code is as follows:

<? Php
Date_default_timezone_set ('prc ');
Sleep (5); // php script sleep for 5 seconds
Echo date ('Y-m-d H: I: S', time (); // Obtain the timestamp of the current system time
Echo'

';
Echo date ('Y-m-d H: I: S', $ _ SERVER ['request _ time']); // Obtain the timestamp when requesting this php script
?>

The output result is as follows:

Conclusion:

Because sleep (5) sleeps for 5 seconds, and then uses time () to obtain the system timestamp, $ _ SERVER ['request _ time'] records the timestamp at which the REQUEST is initiated. Therefore, $ _ SERVER ['request _ time'] is 5 seconds earlier than the timestamp obtained using TIME ().

I hope this article will help you with php programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.