Home >Backend Development >PHP Tutorial >Detailed explanation of PHP time() function to obtain the current timestamp instance
We learned about the mktime() function earlier, and we know that the mktime() function returns a UNIX timestamp based on the given parameters. When the mktime() function does not fill in the parameters, it returns the current timestamp
as follows:
<?php $now=mktime(); echo $now; ?>
However, the main function of the mktime() function is not to return the current timestamp Time, but formatted time, so in PHP, we are specifically provided with a function to obtain the current timestamp, which is the time() function
# we are going to explain in this chapter.##Usage of time() function
In PHP, the current UNIX timestamp is obtained through the time() function. The return value is from the timestamp epoch (Greenwich Mean Time 1970 1 00:00:00 on the 1st of the month) to the current number of seconds. His syntax is as follows:time()Detailed syntax explanation: The time() function has no parameters and the return value is the integer value of the UNIX timestamp. The usage is as follows:
<?php $now=time(); echo $now; ?>
time() function is to obtain the current timestamp. What is obtained is an integer. We format it as follows
date("Y-m-d H:i:s", time()) ;Let’s use an example to illustrate
Get the current timestamp instance
In this example, we use the time() function to get the current local timestamp, and Format the timestamp and output it. The code is as follows<?php header("Content-type:text/html;charset=utf-8"); //设置编码 $nexttime=time()+(7*24*60*60); //7 days; 24 hours; 60 mins; 60secs echo "Now:".date("Y-m-d")."<br/>"; //输出当前日期 echo 'Next time:'.date("Y-m-d",$nexttime); //输出变量$nexttime的日期 ?>The code running result is as follows: The above is a simple example of using the time() function to obtain the current timestamp, and the date() function is to obtain The current time and date. In the next section, we will explain the usage of the date function.
The above is the detailed content of Detailed explanation of PHP time() function to obtain the current timestamp instance. For more information, please follow other related articles on the PHP Chinese website!