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

Linda Hamilton
Release: 2024-10-18 12:10:04
Original
608 people have browsed it

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>
Copy after login

Now, using this code, when you pass '10:00' as $time, you will correctly obtain:

$startTime = 09:30
$endTime = 11:30
Copy after login

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!

source:php
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template