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

How to convert between time and timestamp in PHP

PHPz
PHPzOriginal
2023-03-29 16:24:44661browse

PHP is a very popular web development language that allows web developers to build dynamic websites quickly and easily. In the web development process, it is often necessary to convert time and timestamps. In this article, we will discuss how to convert between time and timestamp in PHP.

1. Time to timestamp

A timestamp is an integer value representing time, usually starting from 0:00:00 on January 1, 1970, Greenwich Mean Time (GMT) The total number of seconds since the start of the second (also called the "UNIX epoch"). In PHP, you can use the time() function to get the timestamp of the current time. To convert any time to a timestamp, you can use PHP's built-in strtotime() function.

Sample code:

//将时间转换为时间戳
$time = "2021-08-13 12:30:00";
$timestamp = strtotime($time);
echo $timestamp; //1628843400

In this example, we pass the date and time string to the strtotime() function and print the returned timestamp to the screen. Note that the strtotime() function can accept many different types of time strings, including absolute times (such as "2021-08-13 12:30:00") and relative times (such as "1 day" or "next Monday") .

2. Convert timestamp to time

To convert a timestamp into a readable date and time format, you usually need to use the date() function. The date() function uses two parameters to generate a formatted date and time string: the first parameter defines the format of the date and time, and the second parameter is an optional timestamp specifying the date and time to be formatted. . If the second argument is not specified, the function uses the current timestamp.

Sample code:

//将时间戳转换为时间
$timestamp = 1628843400;
$time = date("Y-m-d H:i:s", $timestamp);
echo $time; //2021-08-13 12:30:00

In this example, we use the date() function to convert the timestamp into a readable date and time string. Pass the first parameter a format string that defines how the date and time should be formatted. In this example, we use the format "Y-m-d H:i:s" to represent the year, month, date, hour, minute, and second. The result will be printed as "2021-08-13 12:30:00".

Summary:

In this article, we explored ways to convert between time and timestamp in PHP. Use the strtotime() function to convert any time string to a timestamp, and the date() function to convert a timestamp into a readable date and time format. These features are often an integral part of web development tasks, and I hope this article will be helpful to developers.

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