Home >Backend Development >PHP Tutorial >PHP time function usage example

PHP time function usage example

王林
王林Original
2023-06-20 18:23:561084browse

PHP time function usage example

In PHP, timestamp is a very important concept. A timestamp is a marker that represents a specific date and time. PHP provides many time functions, which can easily perform timestamp conversion, date formatting, etc.

This article will introduce some commonly used PHP time functions and provide examples so that readers can better understand the use of this function.

  1. time() function

time() function returns the current Unix timestamp (that is, the number of seconds since 0:00:00 on January 1, 1970 ).

The following is an example:

echo time();

The output result is similar to:

1622825466
  1. date() function

date() function Used to format dates. It takes a timestamp as the first parameter and can return dates in different formats as needed.

The following is an example that formats the current timestamp into a standard date format:

echo date('Y-m-d H:i:s');

The output is similar to:

2021-06-05 09:02:46
  1. strtotime() function

strtotime() function parses a datetime string into a Unix timestamp.

The following is an example:

echo strtotime('2021-06-05 09:02:46');

The output result is similar to:

1622826166
  1. The combination of strtotime() function and date() function

The strtotime() and date() functions are often used together. The following is an example to convert a known date and time string to a specified date format:

echo date('Y年m月d日 H:i:s', strtotime('2021-06-05 09:02:46'));

The output is similar to:

2021年06月05日 09:02:46
  1. mktime() function

The mktime() function is used to return the Unix timestamp of a date.

The following is an example:

echo mktime(0, 0, 0, 6, 1, 2021);

The output is similar to:

1622505600

The above example will return the timestamp of 0:00:00 on June 1, 2021 .

  1. Use the combination of mktime() function and date() function

The mktime() function and date() function are often used together. The following is an example to convert a known date to a specified date format:

echo date('Y年m月d日', mktime(0, 0, 0, 6, 1, 2021));

The output result is similar to:

2021年06月01日

So far, we have introduced some commonly used PHP time functions and usage examples, including time(), date(), strtotime(), and mktime(). I hope these examples can help readers better understand and apply PHP time functions and improve efficiency in actual programming.

The above is the detailed content of PHP time function usage example. 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