• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 113
  • Last Modified:

Create MySQL view instead of static table for xx minute timeslots within a given date period

Hello Experts,

I using PHP/MySQL have the following mysql config table, with the following fields and values - where the values can change periodically;

Column            Value            Desc

Timeslot            20                    minutes

MonOpen      14:00            time open in 24 hour notation
MonClosed      18:00            time closed in 24 hour notation

TueOpen      08:00            time open in 24 hour notation
TueClosed      18:00            time closed in 24 hour notation

WedOpen      08:00            time open in 24 hour notation
WedClosed      18:00            time closed in 24 hour notation

ThuOpen      08:00            time open in 24 hour notation
ThuClosed      19:00            time closed in 24 hour notation

FruOpen            06:20            time open in 24 hour notation
FriClosed      20:00            time closed in 24 hour notation

SatOpen            06:00            time open in 24 hour notation
SatClosed      13:00            time closed in 24 hour notation

SunOpen      00:00            time open in 24 hour notation
SunClosed      00:00            time closed in 24 hour notation

For any date range, I currently create a static table for all available xx mins timeslots.

My question is, given a range of dates, is it possible to create a MySQL view instead as this would stop the table growing over a period of time.

Many thanks

Regards

Jamie
0
Jamie
Asked:
Jamie
  • 2
  • 2
1 Solution
 
Ray PaseurCommented:
I think I would not put this information into a database.  Or if I did, I might extract it and put it into an object like this.  You might store the open/close times and the timeslot duration, not sure exactly how you would want to use those values, so there is a setter method on the object.
https://iconoun.com/demo/temp_jamwales.php
<?php // demo/temp_jamwales.php
/**
 * https://www.experts-exchange.com/questions/28969381/Create-MySQL-view-instead-of-static-table-for-xx-minute-timeslots-within-a-given-date-period.html
 *
 * https://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html
 * https://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL-Procedural-Version.html
 *
 * http://php.net/manual/en/book.datetime.php
 * http://php.net/manual/en/class.datetime.php
 * http://php.net/manual/en/class.dateinterval.php
 *
 */
error_reporting(E_ALL);
echo '<pre>';


Class Day
{
    public $alpha, $omega;
    public function __construct($alpha, $omega)
    {
        $this->alpha = new DateTime('Jan 1, 2001 ' . $alpha);
        $this->omega = new DateTime('Jan 1, 2001 ' . $omega);
    }
}

Class WorkHours
{
    protected $days = [];
    public function __construct($timeslot=20)
    {
        $this->timeslot = $timeslot;
        $this->days['Mon'] = new Day('14:00', '18:00');
        $this->days['Tue'] = new Day('08:00', '18:00');
        $this->days['Wed'] = new Day('08:00', '18:00');
        $this->days['Thu'] = new Day('08:00', '19:00');
        $this->days['Fri'] = new Day('06:20', '20:00');
        $this->days['Sat'] = new Day('06:00', '13:00');
        $this->days['Sun'] = new Day('00:00', '00:00');
    }
    public function setHours($day, $open, $close)
    {
        $this->days[$day] = new Day($open, $close);
    }
    public function getHours($day)
    {
        $hours = $this->days[$day];
        $open  = $hours->alpha->format('g:ia');
        $close = $hours->omega->format('g:ia');
        return [ 'open' => $open, 'close' => $close ];
    }
    public function isOpen($datetime)
    {
        $obj = new DateTime($datetime);
        $day = $obj->format('D');
        $time = $obj->format('H:i');
        $test = new DateTime('Jan 1, 2001 ' . $time);
        if ( ($test >= $this->days[$day]->alpha) && ($test < $this->days[$day]->omega) )
        {
            // COMPUTE THE TIMESLOT
            $diff = $this->days[$day]->alpha->diff($test);
            $hour = $diff->h;
            $mins = $diff->i;
            $appt = $hour * 60 + $mins;
            $slot = (int)floor($appt / $this->timeslot) + 1;
            return $slot;
        }
        return FALSE;
    }
}


$tests =array
( 'Now'
, 'Now + 20 minutes'
, 'Monday 2:00pm'
, 'Monday 2:10pm'
, 'Monday 2:20pm'
, 'Monday 2:30pm'
, 'Monday 1:30pm'
, 'Friday 7:59pm'
, 'Friday 8:00pm'
, 'Friday 8:01pm'
, 'Sunday'
)
;


// SHOW HOW TO USE THE OBJECT
$wh = new WorkHours();


// SHOW HOW TO GET THE WORK HOURS
$test = 'Fri';
echo PHP_EOL . "WORK HOURS ON $test: ";
print_r( $wh->getHours($test) );


// SHOW HOW TO GET THE SLOT FOR A PARTICULAR DATE/TIME VALUE
foreach ($tests as $test)
{
    $res = $wh->isOpen($test);
    if ($res === FALSE)
    {
        echo PHP_EOL . "$test IS OUTSIDE THE WORK HOURS";
    }
    else echo PHP_EOL . "$test IS IN SLOT: $res";
}

Open in new window

Outputs something like:
WORK HOURS ON Fri: Array
(
    [open] => 6:20am
    [close] => 8:00pm
)

Now IS IN SLOT: 23
Now + 20 minutes IS IN SLOT: 24
Monday 2:00pm IS IN SLOT: 1
Monday 2:10pm IS IN SLOT: 1
Monday 2:20pm IS IN SLOT: 2
Monday 2:30pm IS IN SLOT: 2
Monday 1:30pm IS OUTSIDE THE WORK HOURS
Friday 7:59pm IS IN SLOT: 41
Friday 8:00pm IS OUTSIDE THE WORK HOURS
Friday 8:01pm IS OUTSIDE THE WORK HOURS
Sunday IS OUTSIDE THE WORK HOURS

Open in new window

0
 
JamieAuthor Commented:
Hello Ray,

Apologies for the delay in getting back to you.

Many thanks for looking at this and the great solution you have suggested and provided in demo form. Whilst I can see where I would use this, I do need access to the timeslots directly from MySQL as I have other database tables which need to cross reference the static 'timeslots' table - which is ideally why I need it as view instead;

e.g for Monday 19th September to Tuesday 20th September, the 'timeslots' view would contain;

19/09/2016 14:00:00
19/09/2016 14:20:00
19/09/2016 14:40:00
19/09/2016 15:00:00
19/09/2016 15:20:00
19/09/2016 15:40:00
19/09/2016 16:00:00
19/09/2016 16:20:00
19/09/2016 16:40:00
19/09/2016 17:00:00
19/09/2016 17:20:00
19/09/2016 17:40:00
19/09/2016 18:00:00
20/09/2016 08:00:00
20/09/2016 08:20:00
20/09/2016 08:40:00
20/09/2016 09:00:00
20/09/2016 09:20:00
20/09/2016 09:40:00
20/09/2016 10:00:00
20/09/2016 10:20:00
20/09/2016 10:40:00
20/09/2016 11:00:00
20/09/2016 11:20:00
20/09/2016 11:40:00
20/09/2016 12:00:00
20/09/2016 12:20:00
20/09/2016 12:40:00
20/09/2016 13:00:00
20/09/2016 13:20:00
20/09/2016 13:40:00
20/09/2016 14:00:00
20/09/2016 14:20:00
20/09/2016 14:40:00
20/09/2016 15:00:00
20/09/2016 15:20:00
20/09/2016 15:40:00
20/09/2016 16:00:00
20/09/2016 16:20:00
20/09/2016 16:40:00
20/09/2016 17:00:00
20/09/2016 17:20:00
20/09/2016 17:40:00
20/09/2016 18:00:00

As a static table, 1 year of 20 min slots is around 11,000 rows, which would grow by that amount each year. I got a couple of ideas on how to manage the static table through code, but I thought, a view, if possible, would crack this nut in one action.

Many thanks

Regards

Jamie
0
 
Ray PaseurCommented:
Hi, Jamie.  This design sounds like there is a backstory to the application, and it might be better to look at the design from the "50,000 foot view."  The days of the year and the hours of the day are highly predictable and susceptible to generalized programming (even to include DST, holidays, shipping delays, etc) so my instinct is to look for a way to "ask a question" of an API instead of storing all of this information, slot-by-slot in a database.

Some more information is available in this article.  It sounds like you might be looking for a resource scheduling calendar  (search the article for that text).
https://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html

Maybe if you can tell us what you do with this information we can suggest some simpler alternatives.

Also, a table of 11,000 entries, or even 99,000 entries is almost nothing to modern DB servers, if it's correctly indexed!
0
 
JamieAuthor Commented:
Hi Ray,

Many thanks for all your help with this, great advice and article, It has been invaluable, given me the solution and shown other possibilities for my future coding needs - very much appreciated.

Best Regards

Jamie
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now