Opening Hours

3 posts by 2 authors in: Forums > CMS Builder
Last Post: June 14, 2016   (RSS)

By Toledoh - June 7, 2016

Hi All.

I am trying to display the current days opening hours.  

I've got "Standard Days" which are just text fields from the Opening Hours single-record table.

I've got "Special Days" which are records from the Special Days multi-record table which have a date field and content field. 

The code below works to display the text field for standard days, but I need to bypass this if today is a "Special Day".  ie.  If today is Christmas Day, then I want a special message to display rather than the standard.

NB. I'm using only the Day and Month from the Special Day date field, so as to make it "re-occurring". Maybe there's a better way?

// OPENING HOURS

$today = date("l");
$todayDate = date("dm");
$todayHours='';

// find 'special_dates' if today
list($special_datesRecords, $special_datesMetaData) = getRecords(array(
'tableName' => 'special_dates',
'where' => 'date ='.$todayDate, // load first record
'limit' => '1',
));
$special_datesRecord = @$special_datesRecords[0]; // get first record

// find standard opening hours
list($opening_hoursRecords, $opening_hoursMetaData) = getRecords(array(
'tableName' => 'opening_hours',
));
$opening_hoursRecord = @$opening_hoursRecords[0]; // get first record

// Display special hours, or todays hours
if ($special_datesRecords) { $todayHours=$special_datesRecord['content'];}
else{
if ($today=="Sunday") { $todayHours=$opening_hoursRecord['sunday'];}
if ($today=="Monday") { $todayHours=$opening_hoursRecord['monday'];}
if ($today=="Tuesday") { $todayHours=$opening_hoursRecord['tuesday'];}
if ($today=="Wednesday") { $todayHours=$opening_hoursRecord['wednesday'];}
if ($today=="Thursday") { $todayHours=$opening_hoursRecord['thursday'];}
if ($today=="Friday") { $todayHours=$opening_hoursRecord['friday'];}
if ($today=="Saturday") { $todayHours=$opening_hoursRecord['saturday'];}
}

Cheers,

Tim (toledoh.com.au)

By gregThomas - June 14, 2016

Hey Tim, 

I think something like this should work:

<?php

// OPENING HOURS
$today      = date("l");
$todayDate  = date("m-d 00:00:00");
$todayHours ='';

// find 'special_dates' if today
list($special_datesRecords, $special_datesMetaData) = getRecords(array(
'tableName' => 'special_dates',
'where'     => "`date` LIKE '%".$todayDate."'", // load first record
'limit'     => '1',
));
$special_datesRecord = @$special_datesRecords[0]; // get first record

// find standard opening hours 
list($opening_hoursRecords, $opening_hoursMetaData) = getRecords(array(
'tableName' => 'opening_hours',
));
$opening_hoursRecord = @$opening_hoursRecords[0]; // get first record

// Display special hours, or todays hours
if ($special_datesRecords) { $todayHours=$special_datesRecord['content'];}
else{
  $todayHours = $opening_hoursRecord[strtolower($today)];
}

So I've updated the $todayDate variable so that it gets everything but the current year. Then I've added a where statement that will search the special_dates date field for anything that matches on everything but the year. 

Cheers!

Greg

Greg Thomas







PHP Programmer - interactivetools.com