PHP日期打开和关闭脚本 [英] PHP Date Open and Close Script

查看:79
本文介绍了PHP日期打开和关闭脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个任务,使用php创建一个脚本,以在正确的时间显示打开和关闭。到目前为止,我有时间正确地工作,如果在这个时间里这个业务在一周内开放了7天,这将是很好的。然而,该项目的风景是商业开放时间为上午7:00 - 下午5:30,然后星期六上午7:00至下午1:00开放,星期日休息。我以为我可以使用一个日期功能,因为显示0-6,如果

I have a task to create a script using php to display open and closed during the correct times. So far I have the time working correcty and this would be fine if the business was open during this time for 7 days a week. However the scenerio for the project is the business is open mon-fri 7:00am - 5:30 pm then open saturdays 7:00am to 1:00pm and closed sundays. I thought I could use a date function w since is displays 0-6 and call if

if($date >= 0 && $date < 6)

但是没有工作。这是我迄今为止的代码

but that didn't work. Here is the code I have so far

<?php

date_default_timezone_set('America/Chicago');
$open = "700";
$close = "1730";
$time = date('Gi');
$day = date('w');

if ($time >= $open && $time <= $close) {
    echo "We are Open";
} else {
    echo "We are closed";
}

?>


推荐答案

如果您不使用数据库,您可以硬编码每周的每一天都有一些简单易懂的格式:

If you're not using a database you can hardcode each day of the week in some easily parsable format:

$schedule[0] = "700-1730";
$schedule[1] = "700-1730";
$schedule[2] = "700-1730";
$schedule[3] = "700-1730";
$schedule[4] = "700-1730";

$schedule[5] = "700-1300";
$schedule[6] = "0";

$today = $schedule[date('w')];

list($open, $close) = explode('-', $schedule);

$now = (int) date('Gi');

$state = 'Open';

if ($today[0] == 0 || $now < (int) $today[0] || $now > (int) $today[1]) {
  $state = 'Closed';
}

只是写了代码,没有测试它。

Just wrote the code, didn't test it yet.

祝你好运!

这篇关于PHP日期打开和关闭脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆