Home >Backend Development >PHP Problem >How to query the current time in php

How to query the current time in php

青灯夜游
青灯夜游Original
2022-05-05 20:17:2711036browse

5 methods: 1. Use the "echo gmmktime();" statement to output the current GMT time; 2. Use the "echo mktime();" statement; 3. Use "echo microtime();" statement to output the current UNIX timestamp; 4. Use "echo time();" etc.

How to query the current time in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

In date and time functions, UNIX timestamp The acquisition is very important. The timestamp is a character sequence, which refers to the total milliseconds from January 1, 1970, 00:00:00 GMT (08:00:00, January 1, 1970, Beijing time) to the present. number. Here are several functions to get the current time.

1. gmmktime()

gmmktime() function can obtain the UNIX timestamp of the GMT date. The syntax is as follows:

gmmktime(hour,minute,second,month,day,year,is_dst);

The parameters of this function can be left empty from right to left, and the empty parameters will be set to the corresponding current GMT value.

Usage examples are as follows:

echo gmmktime();  //没有设置参数, 则默认取得当前GMT时间

The print result of executing the above program is:

How to query the current time in php

2. mktime()

mktime() can also obtain the UNIX timestamp of a date. The syntax is as follows:

mktime(hour,minute,second,month,day,year,is_dst);

This function returns a UNIX timestamp based on the parameters given. A timestamp is a long integer containing the number of seconds from the UNIX epoch to a given time.

Like the gmmktime() function, the parameters of this function can also be omitted from right to left. Any omitted parameters will be set to the current value of the local date and time.

Usage examples are as follows:

echo mktime();  // 没有设置参数则默认取得当前GMT时间

The print result of executing the above program is:

How to query the current time in php

3. microtime()

microtime() can obtain the current UNIX timestamp and microseconds. The syntax is as follows:

microtime(get_as_float);

If you set the get_as_float parameter value to true, microtime() will return a floating point number; if there is no parameter, it will return a string in the "msec sec" format, where sec is the UNIX epoch since The number of seconds since now, msec is the microsecond part. Both parts of the string are returned in seconds.

Usage examples are as follows:

<?php
echo microtime();  //返回 msec sec 格式字符串表示时间
echo "<br/>";
echo microtime(true);  //返回一个浮点型字符串表示时间
?>

The printing result of executing the above program is as follows:

How to query the current time in php

4. time()

time() function can return the current UNIX timestamp. The syntax is as follows:

time ()

time() The syntax of the function is relatively simple. The usage examples are as follows:

##
复制纯文本复制

执行以上程序的打印结果为:

How to query the current time in php

5、getdate()

getdate() 可取得日期时间信息。语法如下:

getdate(timestamp);

该函数返回一个根据 timestamp 得出的包含有日期信息的关联数组 array。如果没有给出时间戳,则认为是当前本地时间(此时和 time() 函数取值相同)。

其返回的关联数组中的键名单元如表所示。

            getdate() 函数返回关联数组键名
                    键名                     说明                     返回值例子
                    seconds                     秒的数字表示                     0 到 59
                    minutes                     分钟的数字表示                     0 到 59
                    hours                     小时的数字表示                     0 到 23
                    mday                     月份中第几天的数字表示                     1 到 31
                    wday                     星期中第几天的数字表示                     0(周日)到6(周六)
                    mon                     月份的数字表示                     1 到 12
                    year                     4 位数字表示的完整年份                     比如:1999 或 2019
                    yday                     一年中第几天的数字表示                     0 到 365
                    weekday                     星期几的完整文本表示                     Monday 到 Sunday
                    month                     月份的完整文本表示,,比如 January 或 April                     January 到 December
                    0                     自从 UNIX 纪元开始至今的秒数,和 time() 的返回值以及用于 date() 的值类似                     系统相关,典型值为 -2147483648 ~ 2147483647


getdate() 函数的使用示例如下:

<?php
echo "<pre class="brush:php;toolbar:false">";
var_dump(getdate());
?>

执行以上程序的打印结果如下:

How to query the current time in php

推荐学习:《PHP视频教程

The above is the detailed content of How to query the current time in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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