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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • daknightuk
    New Member
    • Jan 2008
    • 10

    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:

    Code:
     $timestamp = time();
     $day = date("D",$timestamp);
     echo $day;
    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
  • daknightuk
    New Member
    • Jan 2008
    • 10

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

    Code:
            	  <?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
    			$timestamp = time();
    			$currentday = date("D",$timestamp);
    			$currenttime=date('H:i');
    			$output="OPEN NOW FOR DELIVERY"; // assumes that the business is open unless one of the statements below picks up it is outside opening hours
    		
    			if ($currentday=="Mon")	
    				{
                		if ($row_Find['MONCLOSE1'] != $row_Find['MONOPEN2'])    		// picks up whether there is actually a lunch break
    						{
    							if ($currenttime > $row_Find['MONCLOSE1'] and $currenttime < $row_Find['MONOPEN2']) $output="LUNCH BREAK"; 
    						}
    					if ($currenttime > $row_Find['MONCLOSE2']) $output="PAST FINAL CLOSING TIME"; 
    					if ($currenttime < $row_Find['MONOPEN1']) $output="NOT OPEN YET"; 
    									
    					echo "The day is Monday";
    					
    					echo $output;	 	
    					echo " The time is : ";
    					echo $currenttime;
    				}
                 
    			 if ($currentday=="Tue")	
    				{
                		if ($row_Find['TUECLOSE1'] != $row_Find['TUEOPEN2'])    		// picks up whether there is actually a lunch break
    						{
    							if ($currenttime > $row_Find['TUECLOSE1'] and $currenttime < $row_Find['TUEOPEN2']) $output="LUNCH BREAK"; 
    						}
    					if ($currenttime > $row_Find['TUECLOSE2']) $output="PAST FINAL CLOSING TIME"; 
    					if ($currenttime < $row_Find['TUEOPEN1']) $output="NOT OPEN YET"; 
    									
    					echo "The day is Tuesday";
    					
    					echo $output;	 	
    					echo " The time is : ";
    					echo $currenttime;
    				}
    			 
    			 if ($currentday=="Wed")	
    				{
                		if ($row_Find['WEDCLOSE1'] != $row_Find['WEDOPEN2'])    		// picks up whether there is actually a lunch break
    						{
    							if ($currenttime > $row_Find['WEDCLOSE1'] and $currenttime < $row_Find['WEDOPEN2']) $output="LUNCH BREAK"; 
    						}
    					if ($currenttime > $row_Find['WEDCLOSE2']) $output="PAST FINAL CLOSING TIME"; 
    					if ($currenttime < $row_Find['WEDOPEN1']) $output="NOT OPEN YET"; 
    									
    					echo "The day is Wednesday";
    					
    					echo $output;	 	
    					echo " The time is : ";
    					echo $currenttime;
    				}
    			 
    			 if ($currentday=="Thu")	
    				{
                		if ($row_Find['THURSCLOSE1'] != $row_Find['THURSOPEN2'])    		// picks up whether there is actually a lunch break
    						{
    							if ($currenttime > $row_Find['THURSCLOSE1'] and $currenttime < $row_Find['THURSOPEN2']) $output="LUNCH BREAK"; 
    						}
    					if ($currenttime > $row_Find['THURSCLOSE2']) $output="PAST FINAL CLOSING TIME"; 
    					if ($currenttime < $row_Find['THURSOPEN1']) $output="NOT OPEN YET"; 
    									
    					echo "The day is Thursday";
    					
    					echo $output;	 	
    					echo " The time is : ";
    					echo $currenttime;
    				}
    			 			 
    			  if ($currentday=="Fri")	
    				{
                		if ($row_Find['FRICLOSE1'] != $row_Find['FRIOPEN2'])    		// picks up whether there is actually a lunch break
    						{
    							if ($currenttime > $row_Find['FRICLOSE1'] and $currenttime < $row_Find['FRIOPEN2']) $output="LUNCH BREAK"; 
    						}
    					if ($currenttime > $row_Find['FRICLOSE2']) $output="PAST FINAL CLOSING TIME"; 
    					if ($currenttime < $row_Find['FRIOPEN1']) $output="NOT OPEN YET"; 
    									
    					echo "The day is Friday";
    					
    					echo $output;	 	
    					echo " The time is : ";
    					echo $currenttime;
    				}
    			 
    			 if ($currentday=="Sat")	
    				{
                		if ($row_Find['SATCLOSE1'] != $row_Find['SATOPEN2'])    		// picks up whether there is actually a lunch break
    						{
    							if ($currenttime > $row_Find['SATCLOSE1'] and $currenttime < $row_Find['SATOPEN2']) $output="LUNCH BREAK"; 
    						}
    					if ($currenttime > $row_Find['SATCLOSE2']) $output="PAST FINAL CLOSING TIME"; 
    					if ($currenttime < $row_Find['SATOPEN1']) $output="NOT OPEN YET"; 
    									
    					echo "The day is Saturday";
    					
    					echo $output;	 	
    					echo " The time is : ";
    					echo $currenttime;
    				}
    			 
    			  
    			 if ($currentday=="Sun")	
    				{
                		if ($row_Find['SUNCLOSE1'] != $row_Find['SUNOPEN2'])    		// picks up whether there is actually a lunch break
    						{
    							if ($currenttime > $row_Find['SUNCLOSE1'] and $currenttime < $row_Find['SUNOPEN2']) $output="LUNCH BREAK"; 
    						}
    					if ($currenttime > $row_Find['SUNCLOSE2']) $output="PAST FINAL CLOSING TIME"; 
    					if ($currenttime < $row_Find['SUNOPEN1']) $output="NOT OPEN YET"; 
    									
    					echo "The day is Sunday";
    					
    					echo $output;	 	
    					echo " The time is : ";
    					echo $currenttime;
    				}

    Comment

    • sumeetk
      New Member
      • Dec 2007
      • 6

      #3
      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

      Comment

      Working...