PHP Date Time


UNIX timestamp is the PHP date and time on the date of a very important concept, from January 1, 1970 00:00:00 to the current number of seconds.

PHP provides a built-in function time() to get the timestamp of the server's current time.

Example:

echo time();

The example above gets a string of numbers like 1279115455

We can use date() and other functions to format it as we need the time and date format.

Valid timestamps are typically in the range of 20:45:54 December 13, 1901 to January 19, 2039, 03:14:07 GMT(this range corresponds to the minimum and maximum of 32-bit signed integers) . However, from 5.1.0 onwards, has been overcome the range of the years can only be between 1901 to 2038 issues.


Tip

  1. The time() is always the current timestamp, so it is not a fixed value
  2. if the timestamp is negative, from January 1, 1970 00:00:00 push forward

time difference

Timestamps may seem less intuitive, but they are handy when computing time. For example, we have to calculate the time difference, or compare the two time to do so, you can directly compare numbers, do not have to take into account the time of year, month, minute and second conversion. But also the efficiency of the time stamp as integer data will be much faster.

Examples of users who need to re-log in after a certain period of time when they log on to the site:

$expiration = $time() + 2 * 3600; // get the current time delay of 2 hours of time stamp
// The validation code is omitted

Then only the user will be required to compare the current timestamp with $expiration during the event, and if the expiration is greater than $expiration then the user is required to log in again.


Page Execution Time

Calculate the current page execution time example:

// Do something
sleep(3);
// Do something
$running_time = time() - $_SERVER['REQUEST_TIME'];
echo 'page run time:', $running_time, 'seconds';

The execution example output is as follows:

Page run time: 3 seconds

$_SERVER['REQUEST_TIME'] is the current page built-in PHP start running time stamp, the current page at the end of the run time() - $_SERVER['REQUEST_TIME'] is the current page run time

Since this example does not have much more content, the sleep(3) function lets the page execute for 3 seconds.




Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook