Troubleshooting Addition of 30 Minutes to H:i Time Format in PHP
The issue of adding 30 minutes to a time value formatted as H:i in PHP can be resolved by converting the time to a Unix timestamp before performing the addition. This ensures that the time calculation is done correctly.
As mentioned in the question, the original attempt used the strtotime function with the wrong argument. Here's the corrected approach:
<code class="php">$time = strtotime('10:00'); $startTime = date("H:i", strtotime('-30 minutes', $time)); $endTime = date("H:i", strtotime('+30 minutes', $time));</code>
By converting to a timestamp first, the addition of minutes can be performed correctly. This will result in the following output for an input of 10:00:
$startTime = 09:30 $endTime = 11:00
The above is the detailed content of How to Correctly Add 30 Minutes to H:i Time Format in PHP?. For more information, please follow other related articles on the PHP Chinese website!