Home > Article > Backend Development > How to Adjust Time Intervals in PHP When Encountering Extraction Difficulties?
Adjusting Time Intervals in PHP
To adjust time intervals formatted as H:i in PHP, you can encounter difficulty extracting the correct values. Let's delve into how to accurately perform this operation.
In the provided code, the error stemmed from not converting the $time string into a timestamp using the strtotime() function before manipulating it. Here's the correct logic to add or subtract 30 minutes from a given time:
<br>$time = strtotime($time); // Convert to a timestamp<br>$startTime = date("H:i", strtotime('-30 minutes', $time));<br>$endTime = date("H:i", strtotime(' 30 minutes', $time));<br>
By converting $time to a timestamp, you're working with a numeric representation of the time, making it easier to apply time-related functions like strtotime(). The adjusted time intervals can then be formatted using date().
The above is the detailed content of How to Adjust Time Intervals in PHP When Encountering Extraction Difficulties?. For more information, please follow other related articles on the PHP Chinese website!