forked from coryetzkorn/php-store-hours
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
108 lines (92 loc) · 3.19 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<!DOCTYPE html>
<html lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<head>
<title>PHP Store Hours</title>
<style type="text/css">
body {
font-family: 'Helvetica Neue', arial;
text-align: center;
}
table {
font-size: small;
text-align: left;
margin: 100px auto 0 auto;
border-collapse: collapse;
}
td {
padding: 2px 8px;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<h1>Gadgets Inc.</h1>
<h2>Store Hours</h2>
<?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 __DIR__ . '/src/StoreHours.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('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 a 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' => "Yes, we're open! Today's hours are {%hours%}.",
'closed' => "Sorry, we're closed. Today's hours are {%hours%}.",
'closed_all_day' => "Sorry, we're closed today.",
'separator' => " - ",
'join' => " and ",
'format' => "g:ia", // options listed here: http://php.net/manual/en/function.date.php
'hours' => "{%open%}{%separator%}{%closed%}"
);
// Instantiate class
$store_hours = new StoreHours($hours, $exceptions, $template);
// Call render method to output open / closed message
echo '<h3>';
$store_hours->render();
echo '</h3>';
// Display full list of open hours (for a week without exceptions)
echo '<table>';
foreach ($store_hours->hours_this_week() as $days => $hours) {
echo '<tr>';
echo '<td>' . $days . '</td>';
echo '<td>' . $hours . '</td>';
echo '</tr>';
}
echo '</table>';
// Same list, but group days with identical hours
echo '<table>';
foreach ($store_hours->hours_this_week(true) as $days => $hours) {
echo '<tr>';
echo '<td>' . $days . '</td>';
echo '<td>' . $hours . '</td>';
echo '</tr>';
}
echo '</table>';
?>
</body>
</html>