By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,264 Members | 1,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,264 IT Pros & Developers. It's quick & easy.

Php Scripting : Mysql Time format and PHP Calc based on opening hours in database

P: 10
Hi people,

I'm working on a project where I have a MYSQL database containing a database table which has opening hours of a load of businesses in it.

Each business has 4 TIME fields for each day in the format 00:00:00 which are named to indicate the weekday i.e MONOPEN1, MONCLOSE1, MONOPEN2, MONCLOSE2, TUEOPEN1.........

My original idea was to output it as a table in the format

Monday
10:00-12:00
14:00-16:00........... obviously showing 4 times per weekday

I then found that I haven't got room for it on my webpage so I decided that the best way would just to give a visual indication as to whether the business is open or closed.

So I had to determine the day:

I managed that using the code:

Expand|Select|Wrap|Line Numbers
  1.  $timestamp = time();
  2.  $day = date("D",$timestamp);
  3.  echo $day;
  4.  
I can use an IF statement to pick up the day and look at the respective 4 fields but heres what i'm really struggling with:

1. I want to format the times so that they are in the format HH:MM not HH:MM:SS as stored in the mysql database

2. Once I have managed to do that I would like to be able to compare the current time (which I can get using a similar function to the above) to the 4 time values and set another field to "Open" or "Closed" so I can echo it to the screen
I just can't get my head round how to do this as I'm not great with maths and time calculations have just got me totally confused, especially with 4 times to look at each time.

If anyone can help with this I would really appreciate it as I am very new to PHP and MYSQL and am only learning at the moment. I've trawled the web quite a bit to try and work out how to do this and there's so many functions I'm lost!

Thanks

David
Jan 2 '08 #1
Share this Question
Share on Google+
2 Replies


P: 10
heres what i've come up with, maybe someone can suggest where it might not work or a better way of coding it?

Expand|Select|Wrap|Line Numbers
  1.               <?PHP   // this code determines what the current date and time is and compares it to the businesses opening hours (assuming that there is a lunch hour also
  2.             $timestamp = time();
  3.             $currentday = date("D",$timestamp);
  4.             $currenttime=date('H:i');
  5.             $output="OPEN NOW FOR DELIVERY"; // assumes that the business is open unless one of the statements below picks up it is outside opening hours
  6.  
  7.             if ($currentday=="Mon")    
  8.                 {
  9.                     if ($row_Find['MONCLOSE1'] != $row_Find['MONOPEN2'])            // picks up whether there is actually a lunch break
  10.                         {
  11.                             if ($currenttime > $row_Find['MONCLOSE1'] and $currenttime < $row_Find['MONOPEN2']) $output="LUNCH BREAK"; 
  12.                         }
  13.                     if ($currenttime > $row_Find['MONCLOSE2']) $output="PAST FINAL CLOSING TIME"; 
  14.                     if ($currenttime < $row_Find['MONOPEN1']) $output="NOT OPEN YET"; 
  15.  
  16.                     echo "The day is Monday";
  17.  
  18.                     echo $output;         
  19.                     echo " The time is : ";
  20.                     echo $currenttime;
  21.                 }
  22.  
  23.              if ($currentday=="Tue")    
  24.                 {
  25.                     if ($row_Find['TUECLOSE1'] != $row_Find['TUEOPEN2'])            // picks up whether there is actually a lunch break
  26.                         {
  27.                             if ($currenttime > $row_Find['TUECLOSE1'] and $currenttime < $row_Find['TUEOPEN2']) $output="LUNCH BREAK"; 
  28.                         }
  29.                     if ($currenttime > $row_Find['TUECLOSE2']) $output="PAST FINAL CLOSING TIME"; 
  30.                     if ($currenttime < $row_Find['TUEOPEN1']) $output="NOT OPEN YET"; 
  31.  
  32.                     echo "The day is Tuesday";
  33.  
  34.                     echo $output;         
  35.                     echo " The time is : ";
  36.                     echo $currenttime;
  37.                 }
  38.  
  39.              if ($currentday=="Wed")    
  40.                 {
  41.                     if ($row_Find['WEDCLOSE1'] != $row_Find['WEDOPEN2'])            // picks up whether there is actually a lunch break
  42.                         {
  43.                             if ($currenttime > $row_Find['WEDCLOSE1'] and $currenttime < $row_Find['WEDOPEN2']) $output="LUNCH BREAK"; 
  44.                         }
  45.                     if ($currenttime > $row_Find['WEDCLOSE2']) $output="PAST FINAL CLOSING TIME"; 
  46.                     if ($currenttime < $row_Find['WEDOPEN1']) $output="NOT OPEN YET"; 
  47.  
  48.                     echo "The day is Wednesday";
  49.  
  50.                     echo $output;         
  51.                     echo " The time is : ";
  52.                     echo $currenttime;
  53.                 }
  54.  
  55.              if ($currentday=="Thu")    
  56.                 {
  57.                     if ($row_Find['THURSCLOSE1'] != $row_Find['THURSOPEN2'])            // picks up whether there is actually a lunch break
  58.                         {
  59.                             if ($currenttime > $row_Find['THURSCLOSE1'] and $currenttime < $row_Find['THURSOPEN2']) $output="LUNCH BREAK"; 
  60.                         }
  61.                     if ($currenttime > $row_Find['THURSCLOSE2']) $output="PAST FINAL CLOSING TIME"; 
  62.                     if ($currenttime < $row_Find['THURSOPEN1']) $output="NOT OPEN YET"; 
  63.  
  64.                     echo "The day is Thursday";
  65.  
  66.                     echo $output;         
  67.                     echo " The time is : ";
  68.                     echo $currenttime;
  69.                 }
  70.  
  71.               if ($currentday=="Fri")    
  72.                 {
  73.                     if ($row_Find['FRICLOSE1'] != $row_Find['FRIOPEN2'])            // picks up whether there is actually a lunch break
  74.                         {
  75.                             if ($currenttime > $row_Find['FRICLOSE1'] and $currenttime < $row_Find['FRIOPEN2']) $output="LUNCH BREAK"; 
  76.                         }
  77.                     if ($currenttime > $row_Find['FRICLOSE2']) $output="PAST FINAL CLOSING TIME"; 
  78.                     if ($currenttime < $row_Find['FRIOPEN1']) $output="NOT OPEN YET"; 
  79.  
  80.                     echo "The day is Friday";
  81.  
  82.                     echo $output;         
  83.                     echo " The time is : ";
  84.                     echo $currenttime;
  85.                 }
  86.  
  87.              if ($currentday=="Sat")    
  88.                 {
  89.                     if ($row_Find['SATCLOSE1'] != $row_Find['SATOPEN2'])            // picks up whether there is actually a lunch break
  90.                         {
  91.                             if ($currenttime > $row_Find['SATCLOSE1'] and $currenttime < $row_Find['SATOPEN2']) $output="LUNCH BREAK"; 
  92.                         }
  93.                     if ($currenttime > $row_Find['SATCLOSE2']) $output="PAST FINAL CLOSING TIME"; 
  94.                     if ($currenttime < $row_Find['SATOPEN1']) $output="NOT OPEN YET"; 
  95.  
  96.                     echo "The day is Saturday";
  97.  
  98.                     echo $output;         
  99.                     echo " The time is : ";
  100.                     echo $currenttime;
  101.                 }
  102.  
  103.  
  104.              if ($currentday=="Sun")    
  105.                 {
  106.                     if ($row_Find['SUNCLOSE1'] != $row_Find['SUNOPEN2'])            // picks up whether there is actually a lunch break
  107.                         {
  108.                             if ($currenttime > $row_Find['SUNCLOSE1'] and $currenttime < $row_Find['SUNOPEN2']) $output="LUNCH BREAK"; 
  109.                         }
  110.                     if ($currenttime > $row_Find['SUNCLOSE2']) $output="PAST FINAL CLOSING TIME"; 
  111.                     if ($currenttime < $row_Find['SUNOPEN1']) $output="NOT OPEN YET"; 
  112.  
  113.                     echo "The day is Sunday";
  114.  
  115.                     echo $output;         
  116.                     echo " The time is : ";
  117.                     echo $currenttime;
  118.                 }
  119.  
  120.  
Jan 3 '08 #2

P: 6
Hi,

You can use this :

[PHP]$timeval = date('H:i');[/PHP]

This wil produce the time in HH:MM

In the mysql database define the field as "time" data type

Hope this helps

Regards
Sumeet
Jan 3 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.