I have a standard mysql timestamp in this format 2011-11-14 20:06:24 . This timestamp will be added whenever a new record is added to the table with the name lead.
I have two input fields for the user to enter from date and to date in dd/mm/yyyy format. Once the user enters both dates and press a button the values will be passsed to other field to get the records from the table lead which are inserted between the time range.
I tried the below query but its not working
SELECT DATE_FORMAT(added_on, '%d/%m/%Y') as date
FROM lead
WHERE added_on BETWEEN "10/11/2011" AND "14/11/2011"
Use standard format for dates, datetimes and timestamps: '2011-11-14' and not '14/11/2011'.
Use single quotes, not double quotes.
If added_on is a timestamp, you should not use BETWEEN or you'll lose almost all records from the last day because '2011-11-14' will be converted to '2011-11-14 00:00:00'. Use this instead:
WHERE added_on >= '2011-11-10'
AND added_on < '2011-11-15' --- note the "< the next day"
or
WHERE added_on >= '2011-11-10'
AND added_on < ('2011-11-14' + INTERVAL 1 DAY)
You should read carefully the MySQL docs: TIMESTAMP properties page for how timestamps are handled (and auto-inserted, updated) in MySQL and the MySQL docs: Timezone support.
you have to convert the date first in php
you can use "date" function of php to covert the date format according to mysql, as follows:
$converteddate = date('Y-m-d',strtotime($yourposteddate));
This will return date in format, e.g. 2011-08-15 which can be understood by mysql and then use it as normal in mysql.
SELECT DATE_FORMAT(added_on, '%m/%d/%Y') as date
FROM lead
WHERE added_on BETWEEN DATE_FORMAT("20/11/2011",'%m/%d/%Y')
AND DATE_FORMAT("21/11/2011", '%m/%d/%Y');
Related
I have next problem:
My table date format was: LIKE 2017-01-08 18:50:25 (with time).
When i use sql query like
'SELECT date FROM table WHERE date = "2017-01-08"'
My row was empty, i need COUNT all row with same (today) date WITHOUT TIME.
Note, i will not change INSERT date time!
Use DATE() to get the date portion of the datetime field and compare it to today. Use COUNT() to get the number of records that match your query.
SELECT count(*) FROM table WHERE DATE(date) = CURDATE()
You can also replace CURDATE() with NOW(), CURRENT_DATE(), and CURRENT_DATE
You can also use it in the following way
'SELECT date FROM table WHERE date_format(date,'%Y-%m-%d') = "2017-01-08"'
the date_format is mysql function which return date according to your pattern the above pattern only return the Y-m-d from the datetime
I hope it will help you
plz change your statement equal operator to greater than
'SELECT date FROM table WHERE date > "2017-01-08"'
as by default if time portion is not present then it is putting 00:00...
Hi there please help me if you can. Here is my senario:
I have a MySQL database with a column that holds a date in the form of a varchar. The format of the date is the following 29/05/2014 (i.e. d/m/Y).
I'm trying to compare the value of this column with todays date and return any rows where the date is earlier than todays date.
I'm using a php variable to store todays as follows:
$date = date("d/m/Y");
Here is my SQL query:
SELECT * FROM patients WHERE last_seen < '$date'
What gets returned
So what is returned is very unusual (to me). All records where the last_seen "day" is less than todays "day". It seems to be overlooking the month and year. So in other words if I last_seen = "30/05/2014" and todays date is "29/05/2014" this record is still returned.
Does anyone have any ideas what I might be doing wrong here?
Thanks
You really, really shouldn't store dates in a varchar field - use date or datetime or timestamp data type.
That said, sometimes you don't have control over the database and you have to deal with somebody else's bad design decision. In this case, to compare dates, convert the varchar strings to dates and compare them that way. So, in your case, you can have something like this:
$date = date("d/m/Y");
and then
SELECT * FROM patients WHERE str_to_date('last_seen', '%d/%m/%Y') < str_to_date('$date', '%d/%m/%Y')
or simpler
SELECT * FROM patients WHERE date(last_seen) < current_date
This way you are actually comparing dates and not strings containing dates. Naturally, this assumes that all dates are stored in the same format.
EDIT: I just tested the last option - and, apparently, date('30/05/2014') returns NULL on my system (mysql 5.5 on linux), hence I suggest the best way is
SELECT * FROM patients WHERE str_to_date('last_seen', '%d/%m/%Y') < current_date
You need to store your date as DATE or DATETIME in your database.
Then you can use:
SELECT * FROM patients WHERE DATE(last_seen) < CURRENT_DATE
I am creating a website where user can create entertainment programs on a particular venue.I need to check whether there is any program fixed on that particular venue for the start time and end time entered by the user.
I have 2 column names in database named starttime and 'endtime' which contains the value in the form of yyyy-mm-dd hh:mm:ss. I think I need to convert the value stored in the both fields in database to timestamp. But how to write the query.
eg:
SELECT *
FROM table_name
WHERE
timestamp(starttime)>='user entered value'
AND timestamp(endtime)<='user entered value '
Is this query possible.?
If "user entered value" is unix timestamp, you can use UNIX_TIMESTAMP(field) function to convert that field to timestamp. If it's not, you have to first convert it to timestamp using strtotime or similar function. Or you can go the other way, and use datetime value inside the query by converting string to datetime using STR_TO_DATE(date, format) MySQL function.
For instance, if user enters the date in YYYY-mm-dd hh:mm format, use following query:
SELECT * FROM table_name WHERE starttime >= STR_TO_DATE('2012-02-01 12:00', '%Y-%m-%d %H:%i') AND endtime <= STR_TO_DATE('2013-01-01 00:00', '%Y-%m-%d %H:%i')
Personally I like to use UNIX timestamps inside the query, but that's just my quirk.
Why not use unix time? It makes dealing with dates much simpler.
$current_time=time();
This creates an integer.
Your query:
SELECT * FROM table
WHERE starttime
BETWEEN starttime AND endtime
I insert my date with the Now() mysql function, and when I select the date I use this:
DATE_FORMAT(com.date,'%h:%i %d/%m/%Y ')
Basically, the output that I get is the same, no matter what time I enter the input (The date is correct, but the time is incorrect):
12:00 20/11/2011
How can I correct this?!?
Mysql set current timestamp when you use now() function. So it should be correct output when you query by
SELECT DATE_FORMAT(com.date,'%h:%i %d/%m/%Y ')
I assume your com.date column is a timestamp, not a date.
I am trying to select all records in a table which have a date between the current date and 1 month ahead.
The date is stored like this DD-MM-YYYY
And the query I have tried:
SELECT * from tablename WHERE renewalDate BETWEEN DATE_FORMAT(CURDATE(),GET_FORMAT(DATE,'EUR')) AND DATE_ADD(DATE_FORMAT(CURDATE(),GET_FORMAT(DATE,'EUR')), INTERVAL 1 MONTH)
But this does not return the correct results.
Is the date stored in an actual date or datetime field? If it's in a char/varchar field, you won't be able to use the BETWEEN syntax, as mysql will just treat them as fixed strings.