";“半小时”;日期方法的间隔


"Half Hour" Intervals for date Method

这似乎是一个基本问题,我不知道以前是否有人问过:

我有一个脚本中的if语句:

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

基本上,这是一个商业脚本。这与商店何时关门有关。6点钟、7点钟等

该变量仅对小时使用值1-24。然而,一些业务在下午5:30(17:30)、下午6:30(18:30)、关闭

18.5的值是否表示下午6:30如果不是,最简单的输入方法是什么?使用日期函数,我可以添加值1830,它知道我的意思是下午6:30?

编辑:此处没有用户输出。脚本只需要知道在一天中的某个时间抛出一个"开关"。

您可以使用strtotime()

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

如果您希望date函数返回小时和分钟,那么date('H')不会为您执行此操作。你需要date('Hi')。返回一个字符串。以下是完整的代码片段:

<?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";
?>

样本输出:

The time is 1225
The store closes at 12:15
the store is closed

你没有给我们足够的信息来回答这个问题。

首先,你不妨让你的脚本支持所有分钟。。。因为有些商店可能会在6:45关门。

我认为你正在寻找的解决方案是简单地对时间戳进行比较。

最简单的方法是使用strtotime。

我倾向于做这样的事情:

$now = new DateTime();
$closing = new DateTime('19:30');
if ($now < $closing) // not closed

我想你不必担心商店在凌晨关门。