Heim >Backend-Entwicklung >PHP-Tutorial >Wie fügt man in PHP korrekt 30 Minuten zum H:i-Zeitformat hinzu?
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
Das obige ist der detaillierte Inhalt vonWie fügt man in PHP korrekt 30 Minuten zum H:i-Zeitformat hinzu?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!