How to compare date inserted from textbox with date from MYSQL db - php

I want to compare date inserted in textbox with date from MYSQL database.
My idea is to make car rent form (with FROM date - TO DATE), and after submiting that form, query will check if it is possible to rent that specific car in that period. Anyone had anything similar?
Thanks

We run many queries that are Start and End Date dependent.
SELECT * FROM rentCar
WHERE textBox BETWEEN StartDate AND EndDate

Related

How to select data by using range of dates in MySql query?

I have a problem related to MySQL query, I use WAMPServer.
I have data in database which have range of dates but when I select data for example
select * from CHD WHERE addtime>='2018-06-15' and addtime<='2018-06-21';
It displays data from '2018-06-15' to '2018-06-20', data of 2018-06-21 are not displayed even if I do
select * from CHD where addtime='2018-06-21';
is not working
Please anyone can help me
This assumes that your column is of type datetime.
The shorthand version of your date in the filter clause is assumed to be at midnight of the date. Your values that you are attempting to retrieve have times after midnight of that date. You either need to define a timestamp along with the date, or you need to filter by the day after for less than equal to or the day before for greater than equal

MySQL custom Timestamp value

I'm having some troubles dealing with Timestamp data type in MySQL.
I'm saving simple records in my database using a simple DB structure, like:
ID int
Name varchar
Date timestamp
Text varchar
And then retrieve them with something like:
SELECT * FROM table WHERE Date BETWEEN '2013-01-01' AND '2013-06-30'
Everything works fine if I store records letting MySQL fill the Date field with the actual timestamp, for example: 2013-10-04 22:40:02 which means I don't add any value to the Date field in my INSERT query.
But I need to be able to add the date by my self since my application needs to store the date from where the application started, and not the date and time in which the query was sent to the database.
So what I do is I create the same date/time format my Date field uses which is 2013-10-04 22:40:02 and then do a simply insert:
INSERT INTO table (Name, Date, Text)
VALUES ('Peter', '2013-10-04 22:40:02', 'Hello...')
Now, doing it this way I'm unable to bring any result by date using a select query like this one:
SELECT * FROM table WHERE Date BETWEEN '2013-01-01' AND '2013-11-30'
Even if I try to sort results by Date using PHPMyAdmin interface, all the records that contain manually added dates disappear. If I sort them by ID, they re-appear. I checked and the dates and formats are correct. So I have no idea what the problem could be. I'm new at MySQL by the way.
Hope you can give me a hand. Thanks!
Well, I think I found the problem and it has nothing to do with PHP and MySQL, the problem is that I generate the date with JavaScript, and it's giving the wrong month.. :/
Thanks to everyone anyway!

How many days since sign up in mysql with new column

I'd like to add a column to my database which displays the amount of time in days since a user signed up.
Currently I have a field which displays the date they signed up in unix.
Is it possible for the new column to increase its fields by 1 each day?
You can basic SQL to get this information for you dynamically. DATEDIFF() will be what you need:
SELECT
DATEDIFF(CURRENT_DATE, FROM_UNIXTIME(date_signed_up)) AS days_since_signup
FROM
tablename
current time - timestamp_of_registration:
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff
SELECT DATEDIFF(CURTIME(), SIGNUP_DATE);

Display Selected Month Values in mysql PHP

In MYSQL database, I have the date column which contains date in format [29-11-2012].
I would like to display the values based upon month selected by user via php page.
For Example, If user selects OCT then this should display all the other columns for this particular month.
Can you suggest SQL Query for this?
Thanks,
Shail
You can use
WHERE MONTH(STR_TO_DATE(formdate,'%d-%m-%Y'))
like this condition
Basically mysql stores dates in yyyy-mm-dd format only..

check if dates are between date range in sql (check without year)

I am developing a holiday guide site!
I am using a MySQL database in which I store the information about hotels.
I have 4 date fields in order to store the 2 possible seasons that works each hotel (date_from, date_to, date_from_extra, date_to_extra). I store the dates in a format like this: DD/MM, for example: 25/12.
I am trying to check if the date1 of first datepicker of the site and the date2 of the second datepicker are between first range (date_from, date_to) or between second range (date_from_extra, date_to_extra) in the database.
The problem is that I don't use years in database date fields and is difficult to compare dates especially when the first or second season in database doesn't finish at the end of the year but it lasts more, for example (14/11 - 25/02).
Any help?
Thanks a lot!
try this
$todayDate = date("Y-m-d");
$check_date= strtotime($todayDate);
use this varible to check
SELECT table_fiels FROM table_namE WHERE month(check_date) BETWEEN month($your_db_start_date) AND month($your_db_end_date) AND day(check_date) BETWEEN day($your_db_start_date) AND day($your_db_end_date)

Categories