PHP Classes

PHP Store Openings: Determine if a store is open at a given time

Recommend this page to a friend!
  Info   View files Example   View files View files (4)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 179 This week: 1All time: 8,724 This week: 560Up
Version License PHP version Categories
openings 1.2GNU General Publi...5.5.3PHP 5, Time and Date, E-Commerce
Description 

Author

This class can determine if a store is open at a given time.

It can take the regular schedule of time that a store is open during the weekdays and checks if the store would be open now.

The class can also consider exceptions of days that the store is close for instance to consider holidays.

Innovation Award
PHP Programming Innovation award nominee
September 2014
Number 9


Prize: One downloadable e-book of choice by O'Reilly
Most stores follow the same schedule to define when they are open for business.

This class can determine if a given store is open now considering its regular open days and times and holiday definitions.

Manuel Lemos
Picture of meivin123
  Performance   Level  
Name: meivin123 <contact>
Classes: 2 packages by
Country: Germany Germany
Age: ???
All time rank: 3658203 in Germany Germany
Week rank: 411 Up15 in Germany Germany Up
Innovation award
Innovation award
Nominee: 1x

Example

<?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(
   
"24.12.2014-31.12.2014" => array("false")
    );

$openings = new Openings($open, $exceptions);
$single = array("22.09.2014"=>array("08:30-12:30","14:00-20:00"));
$openings->addException($single);
if(
$openings->isOpen()){
    echo
"open";
}
else{
    echo
"closed";
}
?>



Details

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"; } ?>

  Files folder image Files  
File Role Description
Plain text file DateUtils.php Class General DateTime functions
Accessible without login Plain text file index.php Example Basic usage example
Plain text file Openings.php Class Main class
Accessible without login Plain text file README.txt Doc. Tutorial

 Version Control Unique User Downloads Download Rankings  
 0%
Total:179
This week:1
All time:8,724
This week:560Up