473,406 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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
2 3082
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
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

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

Similar topics

0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
6
by: Wolfgang Keller | last post by:
Hello, I'm looking for a spreadsheet application (MacOS X prefered, but Windows, Linux ar available as well) with support for Python scripting (third-party "plug-ins" are ok) and a database...
11
by: christian | last post by:
Hi all, I'm creating a TimeSheet Database, I need to calculate how many hours the Employee works.The problem is that when they enter the time, it doesn't calculate the minutes, it just calculate...
6
by: Mark Reed | last post by:
Hi Guru's, I have created a database to monitor hours I have worked as our payroll department are so crap. I work nights most of the time but occasionally I have to work on days. Between the hours...
1
by: jlee | last post by:
I'm pretty much a newbie on mysql, and I need some help. I am running mysql Ver 12.22 Distrib 4.0.24, for portbld-freebsd5.4 (i386) on a server hosting an active website. The site's developer...
5
by: Marijn | last post by:
Hello everybody, I am new to PHP and working on extending my knowledge of OOP. The posts in this group concerned with whether or not to use an OO approach when programming in PHP is not what I...
2
by: Troy Piggins | last post by:
We have some php pages on our intranet at work that have been working fine for years. As a result, they rarely get edited. The page I'm having trouble with is a timesheet entry page which uses...
18
by: Dirk Hagemann | last post by:
Hello, From a zone-file of a Microsoft Active Directory integrated DNS server I get the date/time of the dynamic update entries in a format, which is as far as I know the hours since january 1st...
3
by: Ethan Furman | last post by:
len wrote: I've never had the (mis?)fortune to work with COBOL -- what are the files like? Fixed format, or something like a dBase III style? I
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.