Home >Backend Development >PHP Tutorial >How to Adjust Time Formats in PHP Using strtotime() and date()?

How to Adjust Time Formats in PHP Using strtotime() and date()?

Linda Hamilton
Linda HamiltonOriginal
2024-10-18 12:10:04661browse

How to Adjust Time Formats in PHP Using strtotime() and date()?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn