Use PHP function "strtotime" to parse English text date and time into UNIX timestamp
When developing web applications, we often encounter the need to parse English text date and time. PHP provides a very practical function "strtotime", which can parse date and time strings in various formats into UNIX timestamps.
UNIX timestamp is an integer in seconds, representing the number of seconds from January 1, 1970 00:00:00 GMT to the specified date and time. This format is very convenient when storing and processing date-time data.
The following is a sample code that demonstrates how to use the "strtotime" function to parse English text date and time into a UNIX timestamp:
$text = "February 10 2022 10:30:00"; $timestamp = strtotime($text); echo "输入的文本日期时间为:" . $text . "<br>"; echo "解析得到的UNIX时间戳为:" . $timestamp;
In the above code, we define a variable $text, An English text date-time string "February 10 2022 10:30:00" is stored. We then use the "strtotime" function to parse this string into a UNIX timestamp and store the result in the variable $timestamp.
Finally, we use the "echo" function to output the original text date and time and the parsed UNIX timestamp to the browser.
The output of this code will be:
输入的文本日期时间为:February 10 2022 10:30:00 解析得到的UNIX时间戳为:1644483000
As you can see, by using the "strtotime" function, we successfully parsed the English text date and time into the corresponding UNIX timestamp.
The "strtotime" function not only supports parsing common date formats, such as "YYYY-MM-DD", "MM/DD/YYYY", but also supports parsing relative time expressions, such as "tomorrow", "next" week", "2 weeks ago", etc.
In addition, this function can also handle some special date and time formats, such as "next Monday", "last Friday of February", etc.
In short, PHP's "strtotime" function provides developers with a concise and convenient way to parse English text date and time. By flexibly using this function, we can easily convert date and time strings in various formats into UNIX timestamps, providing our applications with more flexible time processing capabilities.
The above is the detailed content of Use PHP function 'strtotime' to parse English text date and time into UNIX timestamp. For more information, please follow other related articles on the PHP Chinese website!