The PHP code for querying time can be implemented through the time function in PHP. In PHP, you can use the date() function to get the current time.
The following is an example of PHP code for querying time:
<?php // 获取当前时间(默认时区为系统时区) $current_time = date("Y-m-d H:i:s"); echo "当前时间为:".$current_time; ?>
In the above code, the first parameter of the date() function is the time format. Commonly used formats are:
In addition to getting the current time, you can also use strtotime( ) function to convert a string to a Unix timestamp, or use the time() function to get the Unix timestamp of the current time. For example:
<?php // 将字符串转换成Unix时间戳 $timestamp = strtotime("2021-11-11 11:11:11"); echo "Unix时间戳为:".$timestamp; // 获取当前时间的Unix时间戳 $cur_timestamp = time(); echo "当前Unix时间戳为:".$cur_timestamp; ?>
In the above code, the strtotime() function can convert the specified time string into a Unix timestamp, and the time() function can obtain the Unix timestamp of the current time. The Unix timestamp refers to the number of seconds that have elapsed from 0:00:00 on January 1, 1970 to the specified time.
The above is the detailed content of How to write php code to query time. For more information, please follow other related articles on the PHP Chinese website!