langemike/php-store-hours

将 php-store-hours 引入 Laravel

0.3 2018-02-19 11:07 UTC

This package is not auto-updated.

Last update: 2024-09-19 14:16:50 UTC


README

PHP Store Hours 是一个简单的 PHP 类,根据时间OfDay和dayOfWeek输出内容。只需将脚本包含在任何 PHP 页面中,调整每周每天的开业和关门时间,脚本将根据您指定的时段输出内容。

轻松设置每周每天的开业时间

// REQUIRED
// Define daily open hours
// Must be in 24-hour format, separated by dash
// If closed for the day, leave blank (ex. sunday) or don't add line
// If open multiple times in one day, enter time ranges separated by a comma
$hours = array(
    'mon' => array('11:00-20:30'),
    'tue' => array('11:00-13:00', '18:00-20:30'),
    'wed' => array('11:00-20:30'),
    'thu' => array('11:00-1:30'), // Open late
    'fri' => array('11:00-20:30'),
    'sat' => array('11:00-20:00'),
    'sun' => array() // Closed all day
);

添加特定日期/假期的例外情况

// OPTIONAL
// Add exceptions (great for holidays etc.)
// MUST be in format month/day[/year] or year-month-day
// Do not include the year if the exception repeats annually
$exceptions = array(
    '2/24'  => array('11:00-18:00'),
    '10/18' => array('11:00-16:00', '18:00-20:30')
);

使用简码自定义最终输出

选择您希望输出的内容,如果您目前开门、目前关门或全天关门。简码将为您的开或关消息添加动态时间。

// OPTIONAL
// Place HTML for output below. This is what will show in the browser.
// Use {%hours%} shortcode to add dynamic times to your open or closed message.
$template = array(
    'open'           => "<h3>Yes, we're open! Today's hours are {%hours%}.</h3>",
    'closed'         => "<h3>Sorry, we're closed. Today's hours are {%hours%}.</h3>",
    'closed_all_day' => "<h3>Sorry, we're closed today.</h3>",
    'separator'      => " - ",
    'join'           => " and ",
    'format'         => "g:ia", // options listed here: https://php.ac.cn/manual/en/function.date.php
    'hours'          => "{%open%}{%separator%}{%closed%}"
);

可用方法

render([timestamp = time()])

这是默认方法,输出模板内容。您很可能想使用这个。

$store_hours = new StoreHours($hours, $exceptions, $template);
$store_hours->render();

hours_overview([groupSameDays = false])

这返回一个包含完整营业时间列表的数组(无例外情况的一周)。具有相同时间的日子将被分组。

$store_hours = new StoreHours($hours, $exceptions, $template);

echo '<table>';
foreach ($store_hours->hours_overview() as $days => $hours) {
    echo '<tr>';
    echo '<td>' . $days . '</td>';
    echo '<td>' . $hours . '</td>';
    echo '</tr>';
}
echo '</table>';

hours_today([timestamp = time()])

这返回一个包含当天营业时间的数组。

$store_hours = new StoreHours($hours, $exceptions, $template);
$store_hours->hours_today();

is_open([timestamp = time()])

这根据商店是否目前开门返回 true/false。

$store_hours = new StoreHours($hours, $exceptions, $template);
$store_hours->is_open();

用例

多个商店/时间表

如果您想在同一页面上显示多个时间表,只需调用两个独立的 StoreHours 实例。请记住,在每个新实例之前设置时区。

// New York Hours
date_default_timezone_set('America/New_York');
$nyc_store_hours = new StoreHours($nyc_hours, $nyc_exceptions, $nyc_template);
$nyc_store_hours->render();

// Los Angeles Hours
date_default_timezone_set('America/Los_Angeles');
$la_store_hours = new StoreHours($la_hours, $la_exceptions, $la_template);
$la_store_hours->render();

测试

$ phpunit

故障排除

如果您遇到错误或时间未按预期渲染,请在GitHub上提交问题之前请检查这些项目

  • 确保您的时区已配置
  • 确保所有例外情况都使用月份/日期格式
  • 请核实 StoreHours.class.php 是否已在页面上正确包含

请在此GitHub上报告任何错误或问题。我很乐意听到您改进此脚本的想法或看到您在最新项目中如何使用它。

使用 PHP Store Hours 的网站

致谢

归功于 Cory Etzkorn 为开发 php-store-hours