Home  >  Article  >  Backend Development  >  [Summary] Some common methods of operating timestamps in PHP

[Summary] Some common methods of operating timestamps in PHP

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

In PHP, the timestamp is a ten-digit integer that represents the number of seconds that have elapsed since 00:00:00 on January 1, 1970. The "time()" function in PHP can get the current timestamp, and some other functions, such as "strtotime()", can also convert a date and time string into a timestamp.

However, in actual development, sometimes we only need the time part of the timestamp, not the date. At this time, we need to perform some operations on the timestamp to obtain the time information we need.

Below, we will introduce some common PHP methods to operate timestamps so that we can better handle timestamps:

  1. Get the timestamp of the day

We can get the timestamp of the day by removing the date part of the timestamp by taking the integer part. The code is as follows:

$current_time = time(); // 获取当前时间戳
$today_time = intval($current_time/86400)*86400; // 取整数部分得到当天时间戳

In the above code, divide the timestamp by the number of seconds in a day (86400) , then take the integer part, and finally multiply it by the number of seconds in a day to get the timestamp of the day.

  1. Get hours, minutes, seconds

If you need to get the specific time part of the timestamp, we can use the date() function, as shown below:

$current_time = time(); // 获取当前时间戳
$hour = date('H', $current_time); // 获取小时
$minute = date('i', $current_time); // 获取分钟
$second = date('s', $current_time); // 获取秒

In the above code, use the date() function, pass in the timestamp in the second parameter, and then specify the format of the return time in the first parameter to get the required time part.

  1. Get the timestamp of today, yesterday or tomorrow

We can get the timestamp of today, yesterday or tomorrow through the addition and subtraction of timestamps. The code is as follows :

$current_time = time(); // 获取当前时间戳
$today_time = strtotime(date('Y-m-d', $current_time)); // 获取当天时间戳
$yesterday_time = $today_time - 86400; // 获取昨天时间戳
$tomorrow_time = $today_time + 86400; // 获取明天时间戳

In the above code, the date() function is used to obtain the current date string, and then the strtotime() function is used to convert the date string into a timestamp. Finally, yesterday or tomorrow's time is obtained through addition and subtraction operations. stamp.

  1. Calculate time interval

If we need to calculate the time interval between two timestamps, we can use the time() function to get the current timestamp, and then use two The timestamps are subtracted, as shown below:

$timestamp1 = time(); // 获取时间戳1
// 程序执行
$timestamp2 = time(); // 获取时间戳2
$interval = $timestamp2 - $timestamp1; // 计算时间间隔
echo $interval; // 输出时间间隔

In the above code, after getting timestamp 1, there is a period of time during program execution, and then timestamp 2 is obtained, and finally the time is obtained by subtracting the two. interval.

Summary

The above are some common PHP methods for operating timestamps. Through these methods, we can better handle timestamps and improve code efficiency and accuracy.

The above is the detailed content of [Summary] Some common methods of operating timestamps 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