Home  >  Article  >  Backend Development  >  How to convert string to timestamp in PHP

How to convert string to timestamp in PHP

PHPz
PHPzOriginal
2023-03-29 16:16:38517browse

With the continuous development of computer technology, we increasingly need to use various programming languages ​​​​to solve practical problems. Among them, PHP is a language widely used in web development and has a wide range of applications in both enterprises and personal projects. In PHP, converting string to timestamp is a common operation. This article will introduce how to implement the function of converting string to timestamp in PHP.

1. The concept of timestamp in PHP

In PHP, timestamp (timestamp) is an integer representing a certain point in time, accurate to the second level. The timestamp is incremented every second since Unix time. Unix time is the total number of seconds since January 1, 1970 00:00:00 (Greenwich Mean Time).

PHP provides the time() function to get the timestamp of the current time, for example:

$timeStamp = time();
echo $timeStamp; // 输出当前时间的时间戳

2. String to timestamp function in PHP

In PHP, we can use the strtotime() function to convert a string into a timestamp. The prototype of this function is as follows:

int strtotime(string $time [, int $now = time() ]);

Among them, $time is the time string that needs to be converted, $now is an optional parameter, indicating the timestamp of the current time , the default timestamp is the timestamp of the current time.

If $time is a legal date string, then the strtotime() function will return the timestamp of the date, for example:

$timestamp = strtotime('2021-09-28');
echo $timestamp; // 输出 1632777600

If $time cannot be converted to a timestamp, the strtotime() function will return false, for example:

$timestamp = strtotime('not a valid date');
var_dump($timestamp); // 输出 bool(false)

In addition to the date string, strtotime() The function can also handle relative time strings, for example:

$timestamp = strtotime('now');
echo $timestamp; // 输出当前时间的时间戳

$timestamp = strtotime('+1 day');
echo $timestamp; // 输出明天这个时间的时间戳

3. Timestamp to string function in PHP

In PHP, we also You can use the date() function to format the timestamp into a specified date string. The prototype of this function is as follows:

string date(string $format [, int $timestamp = time() ]);

Among them, $format is a date format string, used to specify the format of the date string, $timestamp is an optional parameter , indicating the timestamp that needs to be formatted. The default is the timestamp of the current time.

For example, formatting a timestamp into a string of months and days can be implemented as follows:

$dateString = date('Y-m-d', 1632777600);
echo $dateString; // 输出 2021-09-28

4. Mutual conversion of date strings and timestamps in PHP

We You can combine the strtotime() function and the date() function to achieve mutual conversion between date strings and timestamps. For example, converting a date string into a timestamp can be implemented like this:

$dateString = '2021-09-28';
$timestamp = strtotime($dateString);
echo $timestamp; // 输出 1632777600

Converting a timestamp into a date string can be implemented like this:

$timestamp = 1632777600;
$dateString = date('Y-m-d', $timestamp);
echo $dateString; // 输出 2021-09-28

5. Conclusion

This article This article briefly introduces the method of converting string to timestamp in PHP. In practical applications, we often need to perform various operations on time, such as calculating time differences, formatting time, converting time into different time zones, etc. After understanding the content introduced in this article, readers can better understand and apply these functions.

The above is the detailed content of How to convert string to timestamp in 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