search
HomeBackend DevelopmentPHP ProblemHow to query the current time in php

How to query the current time in php

May 05, 2022 pm 08:17 PM
phpphp date time

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时间
<?php
echo gmmktime();  //没有设置参数, 则默认取得当前GMT时间
echo gmmktime(0,45,3,7,7,2016);  //设置参数表示GMT时间2016年7月7日0点45分3秒
?>

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时间
<?php
echo mktime();  // 没有设置参数则默认取得当前GMT时间
echo mktime(0,45,3,7,7,2016);  //设置参数表示GMT时间2016年7月7日0点45分3秒
?>

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);  //返回一个浮点型字符串表示时间
?>
<?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());
?>
<?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
ACID vs BASE Database: Differences and when to use each.ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PM

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

PHP Secure File Uploads: Preventing file-related vulnerabilities.PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PM

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

PHP Input Validation: Best practices.PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PM

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

PHP API Rate Limiting: Implementation strategies.PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PM

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

PHP Password Hashing: password_hash and password_verify.PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PM

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PM

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP XSS Prevention: How to protect against XSS.PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PM

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

PHP Interface vs Abstract Class: When to use each.PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PM

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment