Home > Article > Backend Development > How to Add or Subtract 30 Minutes to a Time String in PHP?
How to Add and Subtract 30 Minutes to a Time String in PHP
For clarity, you're aiming to manipulate a time value formatted as "H:i" by adding and subtracting 30 minutes. Let's delve into the process.
Defining the Function
<code class="php">$time = '10:00'; // Assuming 'H:i' format // Calculate start and end times $startTime = date("H:i", strtotime('-30 minutes', strtotime($time))); $endTime = date("H:i", strtotime('+30 minutes', strtotime($time)));</code>
Explanation:
Result:
Running this code with an input time of '10:00' will generate the following:
$startTime = '09:30' $endTime = '10:30'
The above is the detailed content of How to Add or Subtract 30 Minutes to a Time String in PHP?. For more information, please follow other related articles on the PHP Chinese website!