Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Half Hour" Intervals for date Method

Tags:

date

php

This seems like a basic question and I don't see if it has been asked before:

I have this if statement in a script:

if (date('H') < $loc_item -> closing) {

Basically it's a script for business. And it has to do with when a store closes. 6 o'clock, 7 o'clock, etc.

The variable uses values 1 - 24 for hour only. However, SOME business close at, 5:30 PM (17:30), 6:30 PM (18:30),

Would a value of 18.5 represent 6:30 PM? If not, what is the simplest way to enter use the date function where I can add a value of 1830 and it knows I mean 6:30PM?

Edit: There is NO user output here. The script just needs to know to throw a "switch" at a certain time of day.

like image 318
TheLettuceMaster Avatar asked Dec 04 '25 06:12

TheLettuceMaster


2 Answers

You could use strtotime()

date("Hi", strtotime("18:40"));
like image 136
edwardmp Avatar answered Dec 05 '25 20:12

edwardmp


If you want the date function to return hours and minutes, then date('H') isn't going to do it for you. You need date('Hi'). That returns a string. The following is a complete code snippet:

<?php
date_default_timezone_set('America/New_York');
echo "The time is ".date('Hi')."\n";
$closingTime = "12:15";
echo "The store closes at ".$closingTime."\n";
if (strtotime(date('Hi')) < strtotime($closingTime)) echo "it is still open\n";
else echo "the store is closed\n";
?>

Sample output:

The time is 1225
The store closes at 12:15
the store is closed
like image 26
Floris Avatar answered Dec 05 '25 18:12

Floris