Determine if the store is open? - php

In PHP and MySQL - how to determine if the Store is Open or Close (return true or false)?
Also how to get the next opening hours if the store is closed?
Example of Opening_Hours table:
+----+---------+----------+-----------+------------+---------+
| id | shop_id | week_day | open_hour | close_hour | enabled |
+----+---------+----------+-----------+------------+---------+
| 1 | 1 | 1 | 16:30:00 | 23:30:00 | 1 |
| 2 | 1 | 2 | 16:30:00 | 23:30:00 | 1 |
| 3 | 1 | 3 | 16:30:00 | 23:30:00 | 0 |
| 4 | 1 | 4 | 16:30:00 | 23:30:00 | 1 |
| 5 | 1 | 5 | 10:00:00 | 13:00:00 | 1 |
| 6 | 1 | 5 | 17:15:00 | 00:30:00 | 1 |
| 7 | 1 | 6 | 17:15:00 | 01:30:00 | 1 |
| 8 | 1 | 7 | 16:30:00 | 23:30:00 | 0 |
+----+---------+----------+-----------+------------+---------+
The open_hour and close_hour are TIME type fields. Table design ok?
Example of current times:
Current time: Tue 23:00, - Output: Open, 'Open at Tue 16:30 - 23:30'
Current time: Tue 23:40, - Output: Close, 'Open at Thur 16:30 - 23:30'
Open on Thursday because Opening_Hours.week_day = 3 is disabled
Now how to handle the midnight time? This get more complicated.
As you can see, on Saturday (Opening_Hours.week_day = 5), it is open from 17:15 PM to 01:30 (closed next day Sunday)
If the current time is Sunday 01:15 AM, then the store would still be open base on Opening_Hours.week_day = 5.
Output: Open, 'Open at Sat 17:15 - 01:30'

In the past, I've handled this by using a time stamp without a date (seconds since midnight). So for Saturday, the open would be 62100 and the close would be 91800.
My thought was this removes some of the logic needed when a close crosses midnight, as you only need to compare the seconds since the start of the date to the time range.
And it's pretty easy to check if it's still open from 'yesterday' - just add 86400 to the current 'time' (seconds since the start of the day) and check against the previous day.
Probably all a single SQL statement.

You can use the PHP date() function and compare it to your opening hours.
You can do something like this recursive function (not working PHP code, but PHP combined with pseudo-code):
/* $current_time should be in the format of date("His") */
function check_hours($current_day, $current_time)
{
Get the MySQL row for today here
if (Opening_Hours.enabled == 1 WHERE Opening_Hours.week_day == $current_day)
{
if ((date("His") >= Opening_Hours.open_hour) and ($current_time <= Opening_Hours.close_hour))
{
// convert_numeric_day_to_full_representation isn't a real function! make one
return 'Open: ' . convert_numeric_day_to_full_representation($current_day) . ' ' . Opening_Hours.open_hour . ' – ' . Opening_Hours.close_hour;
}
elseif (date("His") < Opening_Hours.open_hour)
{
return 'Closed: Next opening hours: ' . convert_numeric_day_to_full_representation($current_day) . ' ' . Opening_Hours.open_hour . ' – ' . Opening_Hours.close_hour;
}
else
{
return check_hours($tomorrow, '000000');
}
}
else
{
return check_hours($tomorrow, '000000');
}
}

Related

how to check booking period is over from current date

I'm having trouble to determine whether booking period is over or not?
i have 2 unix timestamp like this start =>2017-06-20 12:00:00 and End => 2017-06-23 12:00:00
on each time the query is run i want to check whether time is elapsed or not (i,e booking period is reached or not) from my current date (which i can pass using php)
my pseudo code:
select timestampdiff(DAY, '2017-06-20 12:00:00', '2017-06-23 12:00:00'); returns 3
returnedDate = 3; // returns difference either in date format or no.of days
if((returnedDate - mycurrentDate) == 0){
//your booking period is over
}else{
// no of remaining days
}
Desired Solution: i'm looking for mysql specific solution, good php solution is also welcomed.
Question: how to know the pre-booking date is expired?
please help me to solve this, thanks in advance!!!
Assuming mycurrentDate is NOW(), start and end are the dates in which the event occurs, and the threshold for booking is the end date.
You could use DATEDIFF to determine the number of days remaining until the event ends and check if the end date has already passed for the event to show 0.
EG: How many days until the event(s) ends
Demo: http://sqlfiddle.com/#!9/95b548/2
Assuming NOW() is 2017-06-21 12:00:00 using a past, current, and a future events.
| id | start | end |
|----|---------------------|---------------------|
| 1 | 2017-06-20T12:00:00 | 2017-06-23T12:00:00 | #current
| 2 | 2017-06-01T12:00:00 | 2017-06-03T12:00:00 | #past
| 3 | 2017-07-01T12:00:00 | 2017-07-03T12:00:00 | #future
SELECT
id,
NOW() AS `current_date`,
`tn`.`start` AS `starts_on`,
`tn`.`end` AS `ends_on`,
IF(`tn`.`end` >= NOW(),
DATEDIFF(`tn`.`end`, NOW()), #show days until event ends
0 #the event has already passed
) AS `days_remaining`
FROM `table_name` AS `tn`
Results:
| id | current_date | start | end | days_remaining |
|----|---------------------|------------------------|------------------------|----------------|
| 1 | 2017-06-21T12:00:00 | June, 20 2017 12:00:00 | June, 23 2017 12:00:00 | 2 |
| 2 | 2017-06-21T12:00:00 | June, 01 2017 12:00:00 | June, 03 2017 12:00:00 | 0 |
| 3 | 2017-06-21T12:00:00 | July, 01 2017 12:00:00 | July, 03 2017 12:00:00 | 12 |
If end is stored as a unix timestamp, you can use FROM_UNIXTIME() to convert it to a DATETIME value:
IF(FROM_UNIXTIME(`tn`.`end`) >= NOW(),
DATEDIFF(FROM_UNIXTIME(`tn`.`end`), NOW()),
0
) AS `days_remaining`

How to classify a few time-stamp according to identical days?

I have a table like this:
// Times
+----+-------------+
| id | timestamp | // echo date('d/m/Y', $time)
+----+-------------+
| 1 | 1448460315 | // 25/11/2015 -- today
| 2 | 1428440265 | // 07/04/2015
| 3 | 1418160365 | // 09/12/2014
| 4 | 1448460215 | // 25/11/2015 -- today
| 5 | 1438440265 | // 01/08/2015
| 6 | 1438340265 | // 31/07/2015
| 7 | 1438437265 | // 01/08/2015
| 8 | 1448370315 | // 24/11/2015 -- yesterday
| 9 | 1148370315 | // 23/05/2006
| 10 | 1447870315 | // 18/11/2015 -- last week ({11-18}/11/2015)
+----+-------------+
Note: All those number in timestamp column are made of time() function using PHP.
Now I want to know, how can I select all rows which are today Or all rows which are yesterday, or last week?* (it should be noted, in MySQL NOW() is the same with time()).*
For example:
// Times - Today
+----+-------------+
| id | timestamp | // echo date('d/m/Y', $time)
+----+-------------+
| 1 | 1448460315 | // 25/11/2015 -- today
| 4 | 1448460215 | // 25/11/2015 -- today
+----+-------------+
You would need to use the MySQL BETWEEN function
use PHP to get the timestamp from midnight of day to 11:59:59 of day
date_default_timezone_set('America/Chicago');
$day_begins = strtotime(date('Y-m-d 00:00:00', strtotime('today')));
$day_ends = strtotime(date('Y-m-d 11:59:59', strtotime('today')));
-- sql code will look like
SELECT id FROM table WHERE `timestamp` BETWEEN ($day_begins AND $day_ends)
With something like:
SELECT
id,
timestamp
FROM
table
WHERE
DATE_FORMAT(FROM_UNIXTIME(`timestamp`), '%y-%m-%d') = (DATE_FORMAT(NOW(), '%y-%m-%d') - INTERVAL 1 DAY)

How can I get the month interval of a transaction from the last date of it's record to the current date?

How can I get the month interval of a transaction from the last date of it's record to the current date?
Let's look at the table below!
---------------------------------------
|transaction_dates | transaction_time |
---------------------------------------
|2015-01-02 | 11:00:00 |
|2015-01-03 | 12:00:00 |
|2015-02-05 | 7:00:00 |
|2015-03-05 | 10:00:00 |
|2015-03-05 | 19:00:00 |
|2015-04-05 | 13:00:00 |
---------------------------------------
From the current date, August 20, 2015. I don't have any transaction for the month of May,June and July. So by doing some PHP script and SQL, how can I get the months wherein transaction becomes idle to the current date? In the above case, my desired output would be
-------------
|months_idle|
-------------
|5 |
|6 |
|7 |
--------------
Create table with months (1..12) in your DB and select all month > max(MONTH(transaction_dates)) and < MONTH(NOW())

Change content of table according to week number

I am working on a script for a drivers license website, and I need to make a calendar like table for the students, so they can see when they have which lesson.
Right now we are updating the table manually, but I would like to make a script so it can do it automatically.
The table looks like this: (By the way, its a HTML table).
+----------+--------+---------+-----------+----------+--------+
| Week Nr. | Monday | Tuesday | Wednesday | Thursday | Friday |
+----------+--------+---------+-----------+----------+--------+
| 17 | 14 | 1 | 16 | 2 | |
+----------+--------+---------+-----------+----------+--------+
| 18 | 4 | 1 | 6 | | |
+----------+--------+---------+-----------+----------+--------+
| 19 | 8 | 1 | 11 | | |
+----------+--------+---------+-----------+----------+--------+
| 20 | 14 | 1 | 16 | 2 | |
+----------+--------+---------+-----------+----------+--------+
Lets say its this week (Week 17), it has its own set of lessons for every day, except Friday, which is the same for every week. Then it is the week after, which has its own set of lessons, and then it is 3 weeks after, which again has its own set of lessons. Then the 4th week, it start all over, with the same set as week 17, because its a 3 week program, over and over again.
What i want to do is that it automatically updates the table, so it shows the current week number. Then let us say that it is next week now, the table should have automatically update it self to show the current week and its set of lesson numbers.
The numbers under the column "Week Nr." are the week numbers, and the numbers under the day names are the lesson numbers.
So next week it should look like this:
+----------+--------+---------+-----------+----------+--------+
| Week Nr. | Monday | Tuesday | Wednesday | Thursday | Friday |
+----------+--------+---------+-----------+----------+--------+
| 18 | 4 | 1 | 6 | | |
+----------+--------+---------+-----------+----------+--------+
| 19 | 8 | 1 | 11 | | |
+----------+--------+---------+-----------+----------+--------+
| 20 | 14 | 1 | 16 | 2 | |
+----------+--------+---------+-----------+----------+--------+
| 21 | 4 | 1 | 6 | | |
+----------+--------+---------+-----------+----------+--------+
Is there anybody who could give me a hint on how to do that with PHP. I have tried everything I knkw, but I just cant get it right.
This is not exactly what you want, but it could be a good starting point. Just modify it to print out the HTML tags.
//Set a counter for the lessons
$j = 0;
//Loop through the weeks of the year
for ($i = 1; $i <= 52; $i++) {
echo "Week: ".$i."<br>";
echo "This weeks lessons: " . $j."<br>";
//Incrase counter
$j++;
if ($j % 3 === 0) {
//Reset counter if need
echo "<hr>";
$j = 0;
}
}

30 days in Doctrine Symfony

i would like add clause where - max 30 days from now.
in database i have timestamp:
2011-08-30 20:29:35
id | name | date
1 | aaa | 2011-08-30 20:29:35
2 | vvv | 2011-08-10 20:29:35
3 | bbb | 2011-07-10 20:29:35
4 | fff | 2011-08-14 20:29:35
5 | ddd | 2011-06-10 20:29:35
$query = Doctrine_Core::getTable('News')->createQuery('a');
$query->addWhere('date ????????');
How can i get all news recent 30 days?
$query->andWhere('date > ?', date('Y-m-d', time() - 60*60*24*30))
MySQL provides another handy solution :
WHERE date > DATE_SUB(NOW(), INTERVAL 30 DAY)
see : http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add

Categories