/    Sign up×
Community /Pin to ProfileBookmark

Apply special offer prices on monday-friday between 11am and 5pm

I’m working on a website for a restaurant enabling users to order their meals online, pay and then just collect or have delivered. Between Monday and Fridays between the hours of 11:00:00am and 17:00:00pm selected dishes.
I’ve put together a script (below) to verify the current day and the time against the offer times. This works perfectly so far.

I was wondering if anyone can help me possibly come up with something better, perhaps more efficient/ effective or even more streamlined

<?php

$current_time = date(‘H:i:s’);
$lunch_offer_start_time = “11:00:00”;
$lunch_offer_end_time = “17:00:00”;

if(date(‘D’) == ‘Mon’ || date(‘D’) == ‘Tue’ || date(‘D’) == ‘Wed’ || date(‘D’) == ‘Thur’ || date(‘D’) == ‘Fri’) {
if (checkForLunchSpecialDaysAndHours($current_time, $lunch_offer_start_time, $lunch_offer_end_time)):
echo ‘offer, check menu’;
else:
echo ‘no offer available’;
endif;
}

function checkForLunchSpecialDaysAndHours($lunch_offer_start_time, $lunch_offer_end_time, $input) {
$offer_start_time = DateTime::createFromFormat(‘H:i:s’, $lunch_offer_start_time);
$offer_end_time = DateTime::createFromFormat(‘H:i:s’, $lunch_offer_end_time);
$i = DateTime::createFromFormat(‘H:i:s’, $input);

if ($offer_start_time > $offer_end_time)
$offer_end_time -> modify(‘+1 day’);
return ($offer_start_time <= $i && $i <= $offer_end_time) || ($offer_start_time <= $i -> modify(‘+1 day’) && $i <= $offer_end_time);
}
?>

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@rootApr 18.2018 — You should set the timezone so that the server will generate the appropriate time for your service, the visitor will then know if you are open or closed by the server response.

You can use the clients browser to signal to your site the local time on the users machine as this may indicate if the user is actually local to you and if not, figure out their timezone offset, see how far off they are...

I would look in to setting up a database and create a table of the opening hours, make a table that is similar but for offers on the days and times you have them. Also a table to record the orders customers make, a table that holds the zoning, postal code areas you cover...

You could then use SQL to query the database to see if the current TIME() fits in with the opening hours, if it does, the query will have result(s) and these can be further reduced if you use a Day of the Week filter witch you can use to filter out specific days you will be closed.

It would most likely need a joined query and SQL's built in TIME() DATE() type functions to retrieve the results that you can then tell if the shop is open or not because a query should only return a result if the store is open and nothing if it is closed.

This would need the help of someone in the area of GURU status to craft that SQL query.

You are then on the road to be collecting data on how many times your clients repeat their orders, are their any family favourites and this can also balloon in to a system where you inventory track and goods reordering from suppliers can be streamlined


...

So IMHO Go for a Database approach.
Copy linkTweet thisAlerts:
@nsathauthorApr 18.2018 — Ok thanks for that. As you can probably tell, I'm somewhat of a newbie. I would appreciate if you could provide me with some script from which I can carry on from. I'm struggling to get the database schema drawn up. Hence, I jumped that stage and gone into looking at the query for the lunch time offer which I guess would be an update statement on the selected items
Copy linkTweet thisAlerts:
@rootApr 19.2018 — I suggest you check what you have access to on the server to ascertain whethere or not this is a viable avenue to wander down as you would need one of the SQL Shamen and their special kind of voodoo, this would likely be a "joined query" with more than one table depending on the type of data you are trying to get out or put in.

IMHO this would be the way to go because changing opening hours and offers would be as simple as manipulation of the table data and no rewriting of the base code.

A good place to start getting the basics of SQL is to read this and then take each step thereafter : https://www.w3schools.com/sql/sql_syntax.asp then you should be able to understand the power and potential of a database on a site.
×

Success!

Help @nsath spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...