Detailed explanation of PHP time() function to obtain the current timestamp instance

怪我咯
Release: 2023-03-07 14:08:01
Original
7180 people have browsed it

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;
?>
Copy after login

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()
Copy after login

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;
?>
Copy after login

Detailed explanation of PHP time() function to obtain the current timestamp instancetime() 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()) ;
Copy after login

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 &#39;Next time:&#39;.date("Y-m-d",$nexttime);           //输出变量$nexttime的日期
?>
Copy after login
The code running result is as follows:

Detailed explanation of PHP time() function to obtain the current timestamp instance

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template