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`
Related
Say we have a posts table that has the columns: id, title, expires_at. We want to show how many posts where not "expired" in each week of every year.
Maybe a simpler way of putting it would be: "a count of posts grouped by weeks of the year where the expires_at date is great then the start of each week"
For example:
-------------------------------------------------
| Year | Week | posts_not_expired |
------------|-----------|-----------------------|
| 2017 | 01 | 22 |
| 2017 | 02 | 103 |
| 2017 | 03 | 7 |
| ... | ... | ... |
| 2009 | 52 | 63 |
|-----------|-----------|-----------------------|
What we have so far:
SELECT
COUNT(id) as posts_not_expired,
YEAR(expires_at) * 100 as Year,
YEARWEEK(expires_at) as Week,
FROM posts
GROUP BY Year, Week
You can use DAYOFWEEK to count non-expired posts for a given week. (where 1 = Sunday,2=Monday,..7=Saturday)
SELECT
YEAR(expires_at) as `Year`,
WEEKOFYEAR(expires_at) as `Week`,
SUM(DAYOFWEEK(expires_at) > 2) as `posts_not_expired`
FROM posts
GROUP BY YEAR(expires_at), WEEKOFYEAR(expires_at)
This is a question, which I could not find answer to anywhere. Okay. here it is.
I have two date ranges (This month and the last month)
Last month - 01/01/2015 (January 1 2015) to 31/01/2015
This month - 01/02/2015 (1st Feb 2015) to 28/02/2015
Now, each month has weeks. I have a table with column created_at. I want to fetch all the records week-wise into an array (to plot a graph) with their corresponding sum(value) or count(value) .
So it will be something like this:
Last Month:
Week 1 - 25
Week 2 - 34
etc.
This Month:
Week 1: 55
Week 2: 56
etc.
The date is in this format in created_at: 2015-07-21 01:27:14 (Y-m-d H:i:s)
In MySql You can use WEEK() to get the number of the week (from 1 to 53)
O you can use WEEKDAY() or DAYOFWEEK() the first bigins on Monday the second on Sunday.
You can use them into a GROUP BY with HAVING
Something like:
SELECT count(*)
FROM `YourTable`
WHERE `created_at` >= '2015-10-01' AND `created_at`< '2015-11-01'
GROUP BY WEEK(`created_at`)
To use the workaround you found You need to do something similar:
create a table named "numbers" with a field "id" (autoincrement) and 31 rows (one for each day of a month)
Then use a query like this:
SELECT count(i.created_at)
FROM
(SELECT DATE_FORMAT(DATE_ADD('2015-12-01', INTERVAL -n.id DAY), '%Y-%m-%d') AS AllDays
FROM numbers n) AS DaysOfMonth
Left Join
YourTableName i
ON STR_TO_DATE(i.created_at, '%Y-%m-%d') = DaysOfMonth.AllDays
GROUP BY WEEK(AllDays)
(try to adapt it to your needs)
What you need to do is group by the week and then sum the values. Here's a simple example of how it might work:
SELECT DATE_FORMAT(created_at,'%Y-%V') as interval, SUM(units_sold) as total_sold
FROM sales
GROUP BY DATE_FORMAT(created_at,'%Y-%V')
What you'll be getting is the year ant week number (ex. 2015-50) and the sum from that interval.
A table like this:
+----+------------+---------------------+
| id | units_sold | created_at |
+----+------------+---------------------+
| 1 | 2 | 2015-01-01 09:00:00 |
| 2 | 4 | 2015-01-04 10:00:00 |
| 3 | 1 | 2015-01-12 12:00:00 |
| 4 | 4 | 2015-01-16 13:00:00 |
+----+------------+---------------------+
Would result to:
+----------+------------+
| interval | total_sold |
+----------+------------+
| 2015-01 | 6 |
| 2015-03 | 5 |
+----------+------------+
I think it is useful for you...
SELECT GROUP_CONCAT(id), COUNT(id) AS idcount,SUM(id) AS idsum,
MONTHNAME(order_created_date) AS month_name, WEEK(order_created_date)
AS weeks FROM orders GROUP BY WEEK(order_created_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())
I have a table with number of page views per day. Something like this:
+------+------------+------+----------+
| id | date | hits | mangaID |
+------+------------+------+----------+
| 4876 | 1331843400 | 132 | 13 |
+------+------------+------+----------+
| 4876 | 1331929800 | 24 | 236 |
+------+------------+------+----------+
| 7653 | 1331929800 | 324 | 13 |
+------+------------+------+----------+
I'm trying to get sum hits from last week with the below code:
SELECT sum(hits) as hits FROM om_manga_views WHERE DATE_SUB(CURDATE(),INTERVAL 1 week) <= date and mangaID = '13'
My problem is that I'm storing date as time using strtotime in date's field as int type.
So how can i get what i want!?
Try this:
select sum(hits) hitCount from t
where from_unixtime(date) >= current_date() - interval 1 week and mangaId = 11
Here is the fiddle to play with.
I slightly changed your data because the records you provided are older than 7 days, so the sum would return 0.
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');
}
}