Determining if a Time Falls within a Specified Range in PHP
In PHP, you may encounter the need to ascertain whether a given time falls within a specific range, such as between sunrise and sunset. To effectively resolve this issue, follow the steps outlined below:
<code class="php">$current_time = DateTime::createFromFormat('h:i a', $current_time); $sunrise = DateTime::createFromFormat('h:i a', $sunrise); $sunset = DateTime::createFromFormat('h:i a', $sunset);</code>
<code class="php">if ($current_time > $sunrise && $current_time < $sunset) { echo 'Time is between sunrise and sunset'; }<p>For instance, with the provided input:</p> <pre class="brush:php;toolbar:false"><code class="php">$current_time = "4:59 pm"; $sunrise = "5:42 am"; $sunset = "6:26 pm";</code>
The code will output:
Time is between sunrise and sunset
By implementing these steps, you can determine if a given time is within a specified range in PHP.
The above is the detailed content of How to Determine if a Time Falls within a Range in PHP?. For more information, please follow other related articles on the PHP Chinese website!