In PHP development, we often need to format date and time into different forms. Sometimes, we need to remove the seconds from the time and only display the hours and minutes. This is especially common in some scenarios. So, how to quickly remove seconds from a time in PHP? This article will introduce you to some solutions.
Method 1: Use the date() function
In PHP, the date() function is one of the best date/time functions in the world. The date() function can format date/time into any desired form. When outputting time, we can use the date() function to remove the seconds of the time, as shown below:
echo date("H:i", time());
The above code uses the time() function to get the current timestamp and format it into hours and minute format. This way, the output time only contains hours and minutes, not seconds.
In addition, we can also use the strtotime() function to timestamp convert the specified time string so that we can format any time. For example:
$time = "2022-01-01 12:34:56"; echo date("H:i", strtotime($time));
In the above example, we use the strtotime() function to convert the time string into a timestamp, and then use the date() function to format it into hours and minutes.
Method 2: Use the DateTime class
Another method is to use PHP’s DateTime class. This class provides many convenience methods for manipulating dates and times, including formatting dates/times into any desired form.
By creating a DateTime object, we can format the time into the required form. For example:
$datetime = new DateTime(); echo $datetime->format('H:i');
In the above code, we use the format() method to format the time and only display hours and minutes.
We can also create a DateTime object by passing a timestamp, so that any time can be formatted. For example:
$time = 1641189296; $datetime = new DateTime("@$time"); echo $datetime->format("H:i");
The above code creates a DateTime object and converts the timestamp into a readable format, displaying only the hours and minutes. In this example, we used the "@" symbol to tell the DateTime class to parse it as a timestamp.
Method 3: Use the substr() function
Finally, we can also use PHP's substr() function to intercept the hours and minutes in the time string. We can remove the seconds from the time by truncating the time string by 6 characters and output the formatted time. For example:
$time = "12:34:56"; echo substr($time, 0, 5);
In the above example, we use the substr() function to truncate the seconds of the time, leaving only the hours and minutes.
Conclusion
In this article, we introduced three methods to remove seconds from time in PHP. Each method has its advantages and disadvantages, and the specific method of use needs to be determined according to the actual situation. Choose the appropriate method to manipulate date and time according to your needs.
The above is the detailed content of A brief analysis of how to remove seconds from time in php. For more information, please follow other related articles on the PHP Chinese website!