Adjusting Time Formatted as H:i by 30 Minutes in PHP
You mentioned encountering difficulties in adjusting time values formatted as H:i in PHP. Specifically, your attempt to create $startTime and $endTime, offsetting $time by 30 minutes in each direction, was unsuccessful.
To resolve this issue, we'll employ a different approach using strtotime() and date(). Below is the corrected code:
<code class="php">$time = strtotime('10:00'); // Convert the time string to a Unix timestamp $startTime = date("H:i", strtotime('-30 minutes', $time)); // Subtract 30 minutes from the timestamp $endTime = date("H:i", strtotime('+30 minutes', $time)); // Add 30 minutes to the timestamp</code>
Now, using this code, when you pass '10:00' as $time, you will correctly obtain:
$startTime = 09:30 $endTime = 11:30
The above is the detailed content of How to Adjust Time Formats in PHP Using strtotime() and date()?. For more information, please follow other related articles on the PHP Chinese website!