TheLettuceMaster
TheLettuceMaster

Reputation: 15734

"Half Hour" Intervals for date Method

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.

Upvotes: 3

Views: 1034

Answers (4)

Matthew
Matthew

Reputation: 48284

I would tend to do something like:

$now = new DateTime();
$closing = new DateTime('19:30');

if ($now < $closing) // not closed

I assume you don't have to worry about stores closing in the early AM.

Upvotes: 0

Floris
Floris

Reputation: 46375

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

Upvotes: 1

edwardmp
edwardmp

Reputation: 6601

You could use strtotime()

date("Hi", strtotime("18:40"));

Upvotes: 2

hendr1x
hendr1x

Reputation: 1501

You aren't really giving us enough information to answer the question.

First off, you might as well make your script support all minutes...cause some stores might close @ 6:45.

I think the solution you are looking for is to simply do a comparision of timestamps.

The easiest way to do this is using strtotime.

Upvotes: 1

Related Questions