Introduction to the Openings Class
Author: Marvin Petker
+----------------------------------------------+
+ Now available on GitHub +
+ URL: https://github.com/meivin123/Openings/ +
+----------------------------------------------+
Table of contents
0. License
1. Required Files
2. Setting up our script
3. Opening Exceptions
4. Check openings
5. Full example script
----------------------------------------------
0. License
Feel free to use this script for every purpose.
Editing etc. is allowed but no selling.
But please keep my author comment at the top.
Please comment and share you opinions and critics.
----------------------------------------------
1. Required Files
The Openings class only uses own methods and static methods from the DateUtil class.
So the following files are required:
- Openings.php
- DateUtils.php
---------------------------------------------
2. Setting up our script
First of all we need to include the classes.
you can define an autoloader to include the files or just use the following:
<?php
require_once("path/to/Openings.php");
require_once("path/to/DateUtils.php");
?>
An autoloader is recommended.
After we included our files we need to set up our Opening times.
All opening times we pass are PHP arrays.
To define regular opening times we use the following format:
<?php
$openingTimes = array(
"Mon-Fri" => array("08:30-12:30","14:00-19:00"),
"Sat" => array("08:00-12:00"),
"Sun" => array("false")
);
?>
Above you see a basic example for opening times.
An array with each of its key as a day of the week or day range.
Day ranges must be glued with an score (-).
Example: "Mon-Wed", "Sat-Sun", "Fri";
!!! KNOWN ISSUE !!!
Not defined openings for a day (exmpl. you forget Mon) will cause the class
to show this day as opened anytime.
To define the openings you must assign your keys a new array.
Theoretically you can add as much ranges as you want, but I only tested to three.
Back to our array, we add our opening ranges to our second level array.
The format for times is H:i-H:i.
Examples:
<?php
$openings = array(
"Mon-Fri" => array("08:30-12:30","14:00-19:00"),
"Sat" => array("08:30-12:30","14:00-19:00", "20:00-24:00"),
"Sun" => array("false")
);
?>
To define days as closed we use an array with the STRING "false" in it.
<?php "Sun" => array("false") ?>
---------------------------------------------
3. Opening Exceptions:
To add Exceptions we need a diffrent format.
As keys we use now the full date in d.m.Y format.
Ranges are also possible.
We can define an exception array in the same way as the regular opening times.
The only diffrence is that now we use full dates as keys
<?php
$exceptions = array(
//Closed from 24th December to new years eve
"24.12.2014-31.12.2014" => array("false"),
//Opened on January 1st from 10 to 14.
"01.01.2014" => array("10:00-14:00")
);
//Add Exceptions via the constructor
$openings = new Openings($openingTimes, $exceptions);
Or Single exceptions via the addException(Array $eexception) method.
$single = array( "06.12.2014" => array("10:00-12:00") );
$openings->addException($single);
?>
!!! NOTE !!!
Exceptions will override your regular openings in every case.
---------------------------------------------
4. Check if its open.
To check if our store is opened we just need an if-statement and one method call.
<?php
if( $openings->isOpen() ){
echo "Opened";
}
else{
echo "Closed";
}
?>
The class use the current time ("now") to check if the script is executed during
the set opening times.
---------------------------------------------
5. Full Example Script:
<?php
require_once( "classes/Openings.php");
require_once("classes/DateUtils.php");
$open = array(
"Mon-Fri" => array("08:30-12:30","14:00-19:00"),
"Sat" => array("08:30-20:00"),
"Sun" => array("false")
);
$exceptions = array(
"21.09.2014-23.09.2014" => array("08:30-12:30","14:00-20:00")
);
$openings = new Openings($open, $exceptions);
$single = array("22.09.2014"=>array("08:30-12:30","14:00-20:00"));
$openings->addException($other);
if( $openings->isOpen()){
echo "open";
}
else{
echo "closed";
}
?>
|