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:
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:
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!