Home  >  Article  >  Backend Development  >  How to convert between datetime and timestamp in PHP

How to convert between datetime and timestamp in PHP

PHPz
PHPzOriginal
2023-03-29 16:25:15461browse

In PHP, we often need to convert date and time to timestamp, or timestamp to date and time. A timestamp is the number of seconds from January 1, 1970 00:00:00 Coordinated Universal Time (UTC) to the specified time. This article will cover conversion between datetime and timestamp in PHP.

Convert datetime to timestamp

In PHP, you can use the strtotime() function to convert date and time to timestamp. This function accepts a string parameter in datetime format and converts it into a corresponding timestamp. For example:

$date = '2021-01-01 12:00:00';
$timestamp = strtotime($date);
echo $timestamp; // 输出:1609497600

In this example, convert the string in the $date format into a timestamp and assign it to the $timestamp variable. The echo statement will output the timestamp value 1609497600.

It should be noted that the date and time format string accepted by the strtotime() function must be parsable by PHP, otherwise false will be returned. For example, using the following string as a parameter will cause the conversion to fail:

$date = '2021年1月1日';
$timestamp = strtotime($date);
var_dump($timestamp); // 输出:bool(false)

Convert timestamp to datetime

Convert timestamp to date as opposed to converting datetime to timestamp Time is relatively simple. PHP provides the date() function, which can format a timestamp into a datetime string. For example:

$timestamp = 1609497600;
$date = date('Y-m-d H:i:s', $timestamp);
echo $date; // 输出:2021-01-01 12:00:00

In this example, the timestamp 1609497600 is converted into a date and time format string and assigned to the $date variable. The echo statement will output the value of $date 2021-01-01 12:00:00.

Operation timestamp

In PHP, you can also perform arithmetic operations on timestamps, such as adding and subtracting seconds, minutes, hours, days, etc. This operation can be achieved using the strtotime() function in combination with the date() function. For example, to add one day to the timestamp:

$timestamp = 1609497600;
$timestamp = strtotime('+1 day', $timestamp);
$date = date('Y-m-d H:i:s', $timestamp);
echo $date; // 输出:2021-01-02 12:00:00

In this example, use the strtotime() function to add one day to the timestamp and assign the new timestamp to $timestamp Variables. Then, use the date() function to format the new timestamp into a datetime string and assign it to the $date variable. The echo statement will output the value of $date 2021-01-02 12:00:00.

Summary

This article introduces the conversion between datetime and timestamp in PHP. You can use the strtotime() function to convert a datetime to a timestamp, the date() function to convert a timestamp to a datetime, and you can perform arithmetic operations on the timestamp. Note that when using the strtotime() function, you need to ensure that the parameter is a date and time format string that can be parsed by PHP.

The above is the detailed content of How to convert between datetime and 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