我怎样才能加上?’;特别信息;到PHP存储时间插件
我下载了PHP Store hours插件,并尝试在特定日期实现一条消息——以类似的方式,异常也可以工作。但就我个人而言,我无法理解 我们的想法是将一条消息/问候语指定给特定的日期,并在小时以下打印出来 以下是函数文件:我怎样才能加上?’;特别信息;到PHP存储时间插件,php,plugins,Php,Plugins,我下载了PHP Store hours插件,并尝试在特定日期实现一条消息——以类似的方式,异常也可以工作。但就我个人而言,我无法理解 我们的想法是将一条消息/问候语指定给特定的日期,并在小时以下打印出来 以下是函数文件: <?php // ---------------------------------- // PHP STORE HOURS // ---------------------------------- // Version 3.0 // Written by
<?php
// ----------------------------------
// PHP STORE HOURS
// ----------------------------------
// Version 3.0
// Written by Cory Etzkorn
// https://github.com/coryetzkorn/php-store-hours
// DO NOT MODIFY THIS CLASS FILE
class StoreHours {
function __construct($hours = array(), $exceptions = array(), $template = array()) {
$this->hours = $hours;
$this->exceptions = $exceptions;
$this->template = $template;
if(!isset($this->template['open'])) {
$this->template['open'] = "<h3>Yes, we're open! Today's hours are {%hours%}.</h3>";
}
if(!isset($this->template['closed'])) {
$this->template['closed'] = "<h3>Sorry, we're closed. Today's hours are {%hours%}.</h3>";
}
if(!isset($this->template['closed_all_day'])) {
$this->template['closed_all_day'] = "<h3>Sorry, we're closed.</h3>";
}
if(!isset($this->template['separator'])) {
$this->template['separator'] = " - ";
}
if(!isset($this->template['join'])) {
$this->template['join'] = " and ";
}
if(!isset($this->template['format'])) {
$this->template['format'] = "g:ia";
}
if(!isset($this->template['hours'])) {
$this->template['hours'] = "{%open%}{%separator%}{%closed%}";
}
}
// Returns today's hours
public function hours_today() {
$today = strtotime('today midnight');
$day = strtolower(date("D"));
$exceptions = $this->exceptions;
$hours_today = $this->hours[$day];
if($exceptions) {
foreach($exceptions as $ex_day => $ex_hours) {
//echo 'ex' . date('r', strtotime($ex_day + '/2014'));
//echo 'today' . date('r', $today);
if(strtotime($ex_day) == $today) {
// Today is an exception, use alternate hours instead
$hours_today = $ex_hours;
}
}
}
return $hours_today;
}
// Returns boolean
public function is_open() {
$now = strtotime(date("G:i"));
$hours_today = $this->hours_today();
$exceptions = $this->exceptions;
$is_open = 0;
if(!empty($hours_today[0])) {
foreach($hours_today as $range) {
$range = explode("-", $range);
$start = strtotime($range[0]);
$end = strtotime($range[1]);
// Add one day if the end time is past midnight
if($end <= $start) {
$end = strtotime($range[1] . ' + 1 day');
}
if (($start <= $now) && ($end >= $now)) {
$is_open ++;
}
}
}
if($is_open > 0) {
return true;
} else {
return false;
}
}
// Prep HTML
private function render_html($template_name) {
$template = $this->template;
$hours_today = $this->hours_today();
$output = '';
$index = 0;
if(!empty($hours_today[0])) {
$hours_template = '';
foreach($hours_today as $range) {
$range = explode("-", $range);
$start = strtotime($range[0]);
$end = strtotime($range[1]);
if($index >= 1) {
$hours_template .= $template['join'];
}
$hours_template .= $template['hours'];
$hours_template = str_replace('{%open%}', date($template['format'], $start), $hours_template);
$hours_template = str_replace('{%closed%}', date($template['format'], $end), $hours_template);
$hours_template = str_replace('{%separator%}', $template['separator'], $hours_template);
$index ++;
}
$output .= str_replace('{%hours%}', $hours_template, $template[$template_name]);
} else {
$output .= $template['closed_all_day'];
}
echo $output;
}
// Output HTML
public function render() {
if($this->is_open()) {
$this->render_html('open');
} else {
$this->render_html('closed');
}
}
}
?>
实际的问题/错误是什么?我不确定如何处理。我尝试只向异常日期添加一个数组,例如-'12/25'=>array('9:00am-2:00pm','Merry X-mas'),但我会得到一个错误,指出它需要一个整数。同样,这种方式将使我陷入只向异常发送消息而非其他/正常日期发送消息的困境。
<?php
// REQUIRED
// Set your default time zone (listed here: http://php.net/manual/en/timezones.php)
date_default_timezone_set('America/New_York');
// Include the store hours class
require_once('hours.php');
// REQUIRED
// Define daily open hours
// Must be in 24-hour format, separated by dash
// If closed for the day, leave blank (ex. sunday)
// If open multiple times in one day, enter time ranges separated by a comma
$hours = array(
'mon' => array('9:00am - 5:00pm'),
'tue' => array('9:00am - 5:00pm'),
'wed' => array('9:00am - 5:00pm'),
'thu' => array('9:00am - 5:00pm'),
'fri' => array('9:00am - 5:00pm'),
'sat' => array('9:30am - 3:30pm'),
'sun' => array('') // Closed all day
);
// OPTIONAL
// Add exceptions (great for holidays etc.)
// MUST be in format month/day
// Do not include the year if the exception repeats annually
$exceptions = array(
'12/25' => array('9:00am - 1:00pm'),
'1/1' => array('9:00am - 1:00pm')
);
?>
<?php
$template = array(
'open' => "<div class=\"hours-open\">
<p class=\"hours uppercase white\">Yes, we are open!</p>
<p class=\"hours\">From {%hours%}.</p>
</div>",
'closed'=> "<div class=\"hours-closed\">
<p class=\"hours uppercase white\">Sorry, we are closed.</p>
<p class=\"hours\">Today's hours: {%hours%}.</p>
</div>",
'closed_all_day'=> "<div class=\"hours-closed\">
<p class=\"hours uppercase white\">Sorry, we're closed today.</p>
</div>",
'separator' => " - ",
'join' => " and ",
'format' => "g:ia",
'hours' => "{%open%}{%separator%}{%closed%}"
);
$store_hours = new StoreHours($hours, $exceptions, $template);
$store_hours->render();
?>