How to convert time string to timestamp using PHP

PHPz
Release: 2023-03-29 16:22:01
Original
1033 people have browsed it

PHP is a popular Web programming language with a rich function library and convenient and easy-to-use date and time classes. In PHP applications, we often need to process time strings to obtain timestamps for subsequent calculations and comparisons. This article will introduce how to convert a time string into a timestamp.

1. strtotime function

The strtotime function is PHP's built-in date and time processing function, which can convert some common date and time strings into Unix timestamps. For example:

echo strtotime('2022-03-01 12:00:00'); // 1646083200
Copy after login

This function is very convenient and can directly convert the commonly used "year-month-day hour:minute:second" format string into a timestamp. At the same time, the strtotime function also supports relative time format. For example, you can enter strings such as "tomorrow" and "next Monday" to get the corresponding timestamp.

echo strtotime('tomorrow'); // 明天此时时间的时间戳 echo strtotime('next Monday'); // 下个星期一此时时间的时间戳
Copy after login

However, the strtotime function also has some limitations. First, it cannot handle some non-standard date-time formats. For example:

echo strtotime('2022年3月1日 12:00:00'); // false
Copy after login

Although this string also represents a date and time, the strtotime function cannot parse it. Secondly, the strtotime function may get wrong results for some more complex time formats. For example, for some strings that do not contain time zone information, the strtotime function will use the current time zone for parsing by default, which may produce incorrect results.

2. DateTime class

The DateTime class is a date and time object provided by PHP. It provides a wealth of methods for processing date and time, including date and time formatting, time zone conversion, etc. wait. Using the DateTime class when processing time strings, we can ensure that we get the correct timestamp.

$date = new DateTime('2022-03-01 12:00:00'); echo $date->getTimestamp(); // 1646083200
Copy after login

The DateTime class can accept a variety of date and time string formats, and the processing method is very flexible, and can automatically recognize and convert different formats.

In addition to the getTimestamp method, the DateTime class also provides many other methods for outputting date and time strings in different formats, or for adding, subtracting, comparing, and other operations on date and time.

$date = new DateTime('2022-03-01 12:00:00'); echo $date->format('Y-m-d H:i:s'); // '2022-03-01 12:00:00' $date->add(new DateInterval('P2D')); // 添加2天 echo $date->format('Y-m-d H:i:s'); // '2022-03-03 12:00:00' echo ($date > new DateTime('now')); // true
Copy after login

Summary

Whether you use the strtotime function or the DateTime class, you can convert a time string into a timestamp. For simpler time formats, the strtotime function is a better choice because it is very convenient. But if you need to handle more complex date formats, it is recommended to use the DateTime class to obtain higher parsing accuracy.

The above is the detailed content of How to convert time string to timestamp using PHP. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!