Home  >  Article  >  Backend Development  >  How to convert time format to timestamp using PHP

How to convert time format to timestamp using PHP

PHPz
PHPzOriginal
2023-03-24 16:05:321390browse

In PHP, processing time is a very common operation, and time formatting is one of the important operations. Timestamps are a way to represent time, and many functions can be used to manipulate timestamps in PHP. This article will show you how to convert a time format into a timestamp using PHP.

1. Time format and its description

Before dealing with time formatting, we need to first understand the common time formats and their meanings:

  1. Year, month, day, hour, minute and second, such as 2022-01-01 00:00:00.
  2. Time stamp, indicating the number of seconds from January 1, 1970 00:00:00 GMT to the current time, such as 1640995200.
  3. Other formats, such as "January 1, 2022", "2022-01-01", etc.

2. Use the strtotime() function to convert the time format into a timestamp

Use the strtotime() function in PHP to convert the time format into a timestamp. The function is used as follows:

$timestamp = strtotime('2022-01-01 00:00:00');
echo $timestamp; // 输出 1640995200

In the above example, the strtotime() function converts the time string "2022-01-01 00:00:00" into the corresponding timestamp, and the result is 1640995200. This function also supports time strings in other formats, such as "January 1, 2022", "2022-01-01", etc.

3. Use the date() function to convert the timestamp into time format

Use the date() function in PHP to convert the timestamp into time format. How to use the function The method is as follows:

$timestamp = strtotime('2022-01-01 00:00:00');
$datetime = date('Y-m-d H:i:s', $timestamp);
echo $datetime; // 输出 2022-01-01 00:00:00

In the above example, the date() function converts the timestamp $timestamp into the corresponding time format "2022-01-01 00:00:00".

4. Summary

Through the above introduction, we can draw the following conclusions:

  1. PHP can use the strtotime() function Convert the time format into a timestamp, or use the date() function to convert the timestamp into a time format.
  2. strtotime() function supports multiple time formats and can convert time strings into corresponding timestamps.
  3. The date() function can also support multiple time formats, and can output the timestamp to the corresponding time string in the specified format.

When dealing with time formatting, you can choose different functions and formats according to actual needs and use them flexibly in actual development.

The above is the detailed content of How to convert time format to timestamp using 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