Our Script to Display Library Open Hours

 

library hours page

I have looked into ICS calendar format to display our hours on our main web page but the ICS format is a complex mess to script against and calendar apps that use it were not designed to display business hours.

I love scripting for simple plain text tab delimited tables, a format that anyone can easily edit with a spreadsheet program. On our main page I made a javascript that can display one or multiple days of our open hours or display our hours in monthly calendars for the entire year.

For our mobile page I made a much simpler PHP script to display one week. I used PHP because I wanted NON-smart devices to be able to read our hours and since PHP is a server side script any browser can view it.

The table we use has 9 columns because we included Research Desk staffing hours and our late night room with later closing time on the main page hours calendar.

NOTE: In the table the zeros are required when there is no data and the word “Closed” in the Open column changes the format to red text when displayed, “Closed” in the Latenight column blocks the display of latenight closing time.

Day# Date Type Day Open close RefOpen RefClose Latenight
272 5/22/2010 Library Closed Intersession Saturday Closed 0 0 0 Closed
273 5/23/2010 Library Closed Intersession Sunday Closed 0 0 0 Closed
274 5/24/2010 Library Hours Intersession Monday 8am 5pm 0 0 Closed
275 5/25/2010 Library Hours Intersession Tuesday 8am 5pm 0 0 Closed

The mobile PHP script reads today’s date and formats the date exactly as we do in our table then it makes an array of dates containing the next six days. Then it reads the tab delimited text file and runs through the contents of the file and finds each line that contains a date in the array of dates and formats the hours for that day in a line of html including the late night room if it is open.

This script could have been shorter but when I added the section which assigns a different CSS class to odd and even rows I left in some redundancy.

 

 

PHP Script reads dates and displays our open hours

 
<?php
// This routine at the top getts the path to the root of your web site on your server and setts it as a variable
 $fullpath=realpath(basename(getenv("SCRIPT_NAME")));
 $fullpath=str_replace("\\","/",$fullpath);
 $localpath=getenv("SCRIPT_NAME");
$rootpath = str_replace($localpath, "", $fullpath);
?>
<?php include($rootpath .'/m/base/partone.txt'); ?>

<!-- Fill out the Tiltle -->
<title>Library Hours</title>

<?php include($rootpath .'/m/base/parttwo.txt'); ?>

<!-- top of secondary page heading title -->
<h1> Library Hours</h1>

<?php include($rootpath.'/m/base/partthree.txt'); ?>

<!-- This is where you put the content -->
<?php
//error_reporting(E_ALL);
//ini_set('display_errors', '1');

// Explode text file and store each row of the file into the array elements
function explodeRows($data) {
  $rowsArr = explode("\n", $data);
  return $rowsArr;
}

// Explode the columns according to tabs
function explodeTabs($singleLine) {
  $tabsArr = explode("\t", $singleLine);
  return $tabsArr;
}

// Open the text file and get the content
$filename = $rootpath . "/m/LibraryHoursTable.txt";
$handle   = fopen($filename, 'r');
$data     = fread($handle, filesize($filename));
$rowsArr  = explodeRows($data);
$today = date("n/j/Y");

// Make array containing today and seven other dates
echo "<div id=today><p class=center><b>Today:</b> " . date("l n/j/Y") . "<br>\n";
 for($i=1; $i<=6; $i=$i+1)
 {
  $days[$i] = date("n/j/Y", strtotime("+$i day"));
          //echo $days[$i] . " the days<br />";
 }

// Display content which is exploded by regular expression parameters \n and \t
for($i=0;$i<count($rowsArr);$i++) {


  $lineDetails = explodeTabs($rowsArr[$i]);
     if ($lineDetails[1]==$today && $lineDetails[4]=="Closed") {
     $j=0;
       echo "\n <span class=redtext><b>" . " " . $lineDetails[4] . "</b></span></p></div>\n<ul id=hourslist>\n";
      } elseif ($lineDetails[1]==$today) {
     $j=0;
        echo "\n<big><b>Hours:" . " " . $lineDetails[4] . " - " . $lineDetails[5] . " </b></big> \n";
         if (trim($lineDetails[8])!=="Closed"){
          echo "<br /><span class=latehour>Late Room until: " . $lineDetails[8] . "</span>\n";
         }
       echo "</p>\n<ul id=hourslist>\n";
      } elseif (in_array($lineDetails[1], $days) && $lineDetails[4]=="Closed") {
      $j=$j+1;
        if ( $j&1 ){ 
        $oddoreven="hoursodd"; 
        } else { 
        $oddoreven="hourseven"; 
        }
        echo "<li class=" . $oddoreven . "> <b> <span class=redtext> " . $lineDetails[3] . " : " . " " . $lineDetails[4] . "</span></b></li>\n";
      } elseif (in_array($lineDetails[1], $days)) {
      $j=$j+1;
        if ( $j&1 ){ 
        $oddoreven="hoursodd"; 
        } else { 
        $oddoreven="hourseven"; 
        }
        echo "<li class=" . $oddoreven . "> <b> " . $lineDetails[3] . ": </b>" . $lineDetails[4] . " - " . $lineDetails[5]; 
         if (trim($lineDetails[8])!=="Closed"){
          echo "<br /><span class=latehour>&nbsp;&nbsp;&nbsp;Late Room until: " . $lineDetails[8] . "</span>\n";
         }
        echo "</li>\n";
      }
}

echo "</ul>\n ";
?>
<!-- end of your content footer below -->
<?php include($rootpath.'/m/base/partfour.txt'); ?>
Advertisements

About mobilelibraryinacan

Library tech person 4 20 years
This entry was posted in Mobile HTML and CSS, PHP Scripting, The Fun Stuff, Uncategorized and tagged , . Bookmark the permalink.

4 Responses to Our Script to Display Library Open Hours

  1. Sharon Whitfield says:

    I really love this script and I am looking to implement it for my library, but unfortunately I am running into some errors.

    I was wondering if you might be able to assist me in letting me know what these lines of code do:

    01
    08
    09
    10
    11 Library Hours
    12
    13
    14
    15
    16 Library Hours
    17
    18
    19
    20

    • Sorry for the confusion. This code is showing a complete PHP page. What you are seeing on the first 20 lines are set up for the page.
      The routine on 1 – 7 helps the script find the path to files the script uses no matter what kind of server you copy the script to.
      The the other PHP sections before line 20 load the HTML for the page from external files using PHP includes.
      Then there are a few lines of plain HTML. Since we load most HTML from external files using PHP includes we only keep HTML lines that need to be edited like the title in the actual page.

      Lines 20 through 90 are the part that reads the hours table and outputs the list of hours. It is the heart of the script if you add only lines 1-7 and lines 20-90 into one PHP file that would be a simple start for testing.

  2. Sharon WHitfield says:

    I was wondering if it might be possible to gain access to the original php file and txt file. I am having problems getting this to work. I’m not sure if it is the embedded js or the php.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s