Home  >  Article  >  Backend Development  >  How to remove year and month from php timestamp

How to remove year and month from php timestamp

PHPz
PHPzOriginal
2023-03-29 10:12:01572browse

In PHP development, we often use timestamps to record some time-related information, such as user registration time, article publishing time, etc. Sometimes, we need to convert the timestamp into a specific time format, such as "2021-06-30 14:30:00", or only keep the hour, minute or second part of the time. In this article, we will introduce how to remove the year and month from a timestamp in PHP.

The timestamp in PHP is an integer, representing the number of seconds from 0:00:00 on January 1, 1970 (Greenwich Mean Time) to the current time. The commonly used function time() can obtain the current timestamp. We can use the date and time functions provided by PHP to format the timestamp into the time string we need.

For example, we can use the date() function to format the timestamp into a standard date string:

$timestamp = time(); // 获取当前时间戳

$date = date('Y-m-d H:i:s', $timestamp); // 格式化时间戳为日期字符串

echo $date; // 输出: 2021-06-30 14:30:00

In the above code, the first parameter in the date() function is the format A string representing the time format we want to convert the timestamp into. Where, 'Y' represents the four-digit year, 'm' represents the two-digit month, 'd' represents the two-digit date, 'H' represents the hour in the 24-hour clock, 'i' represents the minute, ' s' means seconds.

If we only need to retain the hours, minutes or seconds part of the time, we can modify part of the format string. For example:

$timestamp = time(); // 获取当前时间戳

$hour = date('H', $timestamp); // 只保留小时

$minute = date('i', $timestamp); // 只保留分钟

$second = date('s', $timestamp); // 只保留秒数

echo $hour; // 输出: 14

echo $minute; // 输出: 30

echo $second; // 输出: 00

Of course, if we want to remove the year and month part from the timestamp, we can simply use the date() function and modify the format string to '-m-d H:i:s' :

$timestamp = time(); // 获取当前时间戳

$time = date('-m-d H:i:s', $timestamp); // 去掉年月部分,只保留时分秒

echo $time; // 输出: -06-30 14:30:00

In the above code, we have converted the timestamp into a time format that only retains hours, minutes and seconds, and printed out the results.

To summarize, we can use the date and time functions provided by PHP to format the timestamp into the time string we need. To remove the year and month part of the timestamp, you only need to modify the content in the format string. This method is simple to use and easy to implement.

The above is the detailed content of How to remove year and month from php timestamp. 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