/    Sign up×
Community /Pin to ProfileBookmark

How Can I add “Special Message” to the PHP Store Hours plugin

I downloaded the PHP Store hours plugin and been messing around with it trying to implement a message to certain dates – in a similar fashion and the exceptions work. But for the life of me I can’t figure it out.

The idea would be to assign a message/greeting to a specific date and print it out below the hours.

Here’s the functions file:

[code=php]
<?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’);
}
}

}

?>
[/code]

Here’s what I have it printing out:

[code=php]
<?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();
?>
[/code]

Thank you in advance for any input. Have a great 2016 everyone!

Sergio

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@SribeiroauthorJan 01.2016 — I tried just adding another level to the array of the exceptions dates such as:

[code=php]
'12/25' => array('9:00am - 1:00pm', 'Merry X-mas'),
'1/1' => array('9:00am - 1:00pm', 'Happy New Year')
[/code]


but I get an error stating it was expecting an integer.

Also This way will corner me into only have messages to exceptions and not other/normal days.

If anyone has any suggestions, please let me know.

Thank you all.

Sergio
×

Success!

Help @Sribeiro spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...