Time in PHP

Hi,

I have an on-line ordering system set up for a restaurant. It has a mini cart showing on the main page to show what you have ordered (rather than saying there are ‘x’ items in your basket).

In a specified section, there are 2 php files which have been included.

  • The Mini Cart
  • Open hours image

I want to be able to show the minicart when the restaurant is open (during 6-10pm) in addition to showing the open hours php file. When the restaurant is closed, I want to be able to add a restriction which hides the minicart and only displays the open hours.

Below is some code of what I have started, however I really do not know how to specify time frames in php code as I am quite new to PHP.

[php]

<?php If (/*time is from 6-10pm*/) { require_once 'include/miniCart.php'; include 'include/openHours.php'; } else { include 'include/openHours.php'; } ?>

[/php]

I really would appreciate it if someone could help me out of this pickle, many thanks!

here is an idea create a database field called isopen, then create a cronjob for 6am, have it call a php file which sets the field “isopen” to 1. then create another cron job FOR 10PM have it set is open to 0. then in the script do something like
[php]$
a=mysql_query(“select isopen from yourtable”);
$b=mysql_fetch_assoc($a);
if($b[‘isopen’]=1){
require_once ‘include/miniCart.php’;
include ‘include/openHours.php’;
}else{
include ‘include/openHours.php’;
}[/php]

hope this idea helps you out!!!

Thanks so much for your reply.

I used a snippet of code to define the open and closed times. And then used if and else statements to display the ‘minicart’ and an open sign during open hours and (else) it will show a closed sign during closed hours.

Here is the code I have used to do this:

[php]

<?php require_once 'library/config.php'; require_once 'library/cart-functions.php'; //Sets Time Zone date_default_timezone_set('Europe/London'); // Define daily open hours. Must be in 24-hour format, separated by dash. If closed for the day, set to 00:00-00:00 $time_range_mon = '18:00-22:00'; $time_range_tue = '18:00-22:00'; $time_range_wed = '18:00-22:00'; $time_range_thu = '14:00-22:00'; $time_range_fri = '18:00-23:00'; $time_range_sat = '18:00-23:00'; $time_range_sun = '18:00-22:00'; // Place HTML for output here. Image paths or plain text (H1, H2, p) are all acceptable. $open_output = 'Come in, we\'re open!'; $closed_output = 'Sorry, we\'re closed!'; // OPTIONAL: Output current day's open hours $echo_daily_hours = true; // Switch to FALSE to hide numerical display of current hours $time_output = 'g A'; // Enter custom time output format (options listed here: http://php.net/manual/en/function.date.php) $time_separator = ' - '; // Choose how to indicate range (i.e XX - XX, XX to XX, XX until XX) $closed_text_output = "Closed"; // This will show if $echo_daily_hours is set to true and the current day's time range is set to 00:00-00:00 // Gets current day of week $status_today = strtolower(date("D")); // Gets current time of day in 00:00 format $current_time = date("G:i"); // Makes current time of day computer-readable $current_time_x = strtotime($current_time); // Builds an array, assigning user-defined time ranges to each day of week $all_days = array("mon" => $time_range_mon, "tue" => $time_range_tue, "wed" => $time_range_wed, "thu" => $time_range_thu, "fri" => $time_range_fri, "Sat" => $time_range_sat, "sun" => $time_range_sun); foreach ($all_days as &$each_day) { $each_day = explode("-", $each_day); $each_day[0] = strtotime($each_day[0]); $each_day[1] = strtotime($each_day[1]); } // Defines array of possible days of week $week_days = array("mon", "tue", "wed", "thu", "fri", "sat", "sun"); // Compares current day of week to possible days of week and determines open vs closed output based on current day and time. foreach ($week_days as &$each_week_day) { if ($status_today == $each_week_day) { echo '
'; if (($all_days[$each_week_day][0] <= $current_time_x) && ($all_days[$each_week_day][1] >= $current_time_x)) { require_once 'include/miniCart.php'; echo $open_output; } else { echo $closed_output; } if ($echo_daily_hours) { echo '
'; if ((date("g", $all_days[$each_week_day][0]) == "12") && (date("g", $all_days[$each_week_day][1]) == "12")) { echo $closed_text_output; } else { echo date($time_output, $all_days[$each_week_day][0]) . $time_separator . date($time_output, $all_days[$each_week_day][1]); } echo ''; } echo '
'; } } ?>

[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service