```In the above code, the first parameter of the date() function is the time"/> ```In the above code, the first parameter of the date() function is the time">

Home  >  Article  >  Backend Development  >  How to write php code to query time

How to write php code to query time

PHPz
PHPzOriginal
2023-04-13 09:04:40930browse

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:

  • Y: Four-digit year
  • m: Two-digit month
  • d: Two-digit day
  • H: Two-digit hour ( 24-hour format)
  • i: two-digit minutes
  • s: two-digit seconds

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!

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