473,544 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

10 New Member
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 3088
daknightuk
10 New Member
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
sumeetk
6 New Member
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
3928
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 version of this document can be found at: http://prdownloads.sourceforge.net/souptonuts/README_mysql.txt?download
6
3562
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 interface. Applications that I know of (that they exist) are: MS Excel Quattro
11
6250
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 hours.it rounds up to 6 and not 5:30. I'm using this formula: Me.Hours = (DateDiff("h", Me.Start, Me.Stop)) Ex:
6
6385
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 of 18:00 and 06:00 colleagues get an unsocial shift allowance. I have managed to get it to work out my gross income for a 4 week period accounting...
1
3360
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 uses his own php shopping cart to receive customer orders. The configuration was done via cPanel with no external modifications - which produced...
5
2705
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 want to discuss in this post. Rather I would like to discuss the best way to program the following problem: I have a MySQL Database which for...
2
1459
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 timestamps to record hours spent on different projects in a mysql database. If users try to enter times using the php interface, they are getting...
18
13019
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 1901. For Example: the number 3566839 is 27.11.07 7:00. To calculate this in Excel I use this:...
3
2315
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
7625
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7389
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5928
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5306
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4930
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3427
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1848
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
677
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.