How to check overlapping dates between multiple date ranges? [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Sorry for my bad english, but I have a problem about check duplicate date.
I have a list date includes from_date and to_date in database and I want check like below:
In database:
from_date | end_date
-------------------------
2020/02/10 | 2020/02/15
-------------------------
2020/01/20 | 2020/01/31
-------------------------
2020/02/16 | NULL
-------------------------
Input:
input['from'] = 2020/01/15;
input['to'] = 2020/01/25;
input['from'] = 2020/01/10;
input['to'] = NULL;
NULL meaning the date unlimited after.
Condition:
Check input[from] and input[to] does not overlap with another range in the database.
in this case:
input is invalid:
because
dates in 2020/01/15 - 2020/01/25 overlap with range 2020/01/20 - 2020/01/31
dates in 2020/01/10 - NULL overlap with range 2020/02/16 - NULL
valid case:
2020/02/01 - 2020/02/09
2020/01/10 - 2020/01/19

You can use the following logic to get rows that have any overlap (other than the endpoints) for the parameters:
select d.*
from dates d
where d.from_date < #to and
(d.to_date > #from or d.to_date is null)

Related

How to get the values in between below string using mysql query [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have this value in my mysql table called item.Item is a column name, I want to get the values present in between two date values using query. How to get that.
eg. i want from 2017-2-6 to 2017-02-07 values.
Please guide me. Its a string value
2017-02-06_CHAWAL,2017-02-07_RAJMA,2017-02-08_ROTI,2017-02-09_BENGAN MASALA,2017-02-10_DAAL
You need to have separate table column with date. So you need to split this 2017-02-06_CHAWAL to
| date | name |
| 2017-02-06 | chawal |
Then you, can use between:
SELECT * FROM table WHERE date BETWEEN '2017-02-05' AND '2017-02-07'

The best way to count visits in php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have got the mysql table which includes data about users who visited my web service (IP address, date). The one IP address can be logged in my database only once per day.
What is the best way to create the table contains visits from last month? I want something like:
30.10.2016 | 1457
31.10.2016 | 1604
01.11.2016 | 1590
etc
Sorry for English and maybe the very simple question, first time on stack :)
use this sql code to create a table for last month visitor count.
CREATE TABLE last_mon_visit AS
SELECT DISTINCT date, COUNT(ip_address)
FROM visit
WHERE YEAR(date) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
GROUP BY date
i would suggest to create view instead of table.

convert time to total hours and calculate from mysql data with php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have an html field:
<label for="Time-2">Time :</label>
<input type="number" name="Time-2">
and the DB is as follows:
+----+--------+--------+------------+
| id | Time-1 | Time-2 | Percentage |
+----+--------+--------+------------+
| 1 | 300 | 10 | |
+----+--------+--------+------------+
Here the Time-1 and Time-2 are in minutes
Time-2 from DB will be input from the html, Time -1 values are filled from other html field. When input is given am looking for a php code where in the time(Time -2) has to be converted from hours and minutes to total minutes
and calculate the formula ((Time-1 - Time-2) / Time-1) * 100. and the result is to be inserted in Percentage column of the same row.
Thanks in advance.
If you are asking how to update the table in the database to set the percentage column
UPDATE time_table set percentage = (time1 - time2)*100/time1 WHERE time1 <> 0;
This will update every row in the table so that the percentage is shown. But that begs the question. Do do you really need to save the percentage in the table? The answer is no!!
You can just as easily calculate it at the time of retrieval
SELECT id, time1, time2 , (time1 - time2)*100/time1 as percentage from time_table
So you can actually drop the percentage column!
Finally get onto user input if the time is given as HH:MM the easiest thing to do is to split
$parts = split($input,":");
$time2 = $parts[0] * 60 + $parts[1]

Displaying record between two dates [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to display a database record between two date ranges using PHP-mysql.
I am working on a project in which employees payment record is saved in a table. I want to see what amount has been paid between two dates selected through date-picker.
Try this:
SELECT amount FROM mytable WHERE created_at BETWEEN '2011-12-01' AND '2011-12-07';
Assuming mytable to be the table name, created_at to be the date field and amount to be the amount field.
Hope this helps.
Peace! xD
SELECT amount FROM mytable
WHERE
created_at >= '2011-12-01'
AND
created_at <= '2011-12-07';
If you need the sum of the salary between two dates you can do somethig like:
SELECT sum(salary) as total FROM table_name
WHERE job_date BETWEEN 'dd-mm-yyyy' AND 'dd-mm-yyyy';

php mysql car parking query [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Hey i need a bit mysql query help..
i have a table named airport_car_parking with 4 fields
id [int autoincrement primary key]
email [varchar]
departure_date [DATETIME]
arrival_date [DATETIME]
now i have two variables $departure_date [something in datetime format] and $arrival_date [something in datetime format]
I want to write a query in php - mysql that checks whether my booking days belongs into above table booking dates or not...
say if i consider my above table data like below
id | email | departure_date | arrival_date
1 | abc#gmail.com | 2013-12-22 12.15.00 | 2013-12-25 12.15.00
so no one can book a car in those 3 days of dec 2013
i have two variable like
$departure_date= 2013-12-24 12.15.00 and
$arrival_date= 2013-12-26 12.15.00
but a car can not be park in those days because table data shows that in this time already a car is booked... i need a query that checks any booking plot is available or not.
I write a query like below , but its valid only after existing period
$chk_date_sql="select * from airport_car_parking where return_date >= '$deperture_date'";
Your query is referencing return_date, which isn't a column in the airport_car_parking table.
As for the logic of the query, you want to make sure that the $departure_date isn't between any row's departure_date or arrival_date. I would recommend the following query -
$chk_date_sql="SELECT * FROM airport_car_parking WHERE '$departure_date' BETWEEN departure_date AND arrival_date;";
And then that query returns with any rows, that means that the requested time will not work.

Categories