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
Related
I have a MySQL database. In a couple of tables, the information that gets stored needs to be retrievable by week. So, I want to be able to do a SELECT FROM *database* WHERE week = *week*. The problem that I have is that the week part is stored as a unix timestamp (to allow for more versatilty like getting the date and time, just time, etc...).
So the question: How can I retrieve this record WHERE date = *date* when the stored date is a unix timestamp and date I'm matching it against is not?
If my question is too confusing and something needs to be rephrased or said in a clearer manner please comment and let me know.
MySQL has a built-in WEEK() method for handling dates: MySQL WEEK() Reference
Unfortunately however, MySQL's WEEK() method only supports DATE datatypes rather than a UNIX TIMESTAMP. Therefore, we must first convert the timestamp to a date so we can then pass that date to the WEEK() method:
SELECT
*
FROM
my_table
WHERE
WEEK(
DATE_FORMAT(
FROM_UNIXTIME('my_unix_timestamp_col'),
'%e %b %Y'
)
) = 51
If you have a column which is the DATE data-type, the query can be simplified (and can also use indexes):
SELECT * FROM my_table WHERE WEEK(my_date_col) = 51
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 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');
I am working with a table on which I can't change the structure.... Now there is a varchar column which contains a timestamp. Now I need to select the records whose timestamp translates to the current date, or a specified date.
Any help?
First off you shouldn't be storing date information in a mysql database with a VARCHAR field. Rather use DATETIME that is what it is for. I can only guess how you have stored your timestamp date in the database but I am going to assume it is the following format:
YYYY-mm-dd hh:mi:ss (ie '2011-04-15 09:23:55')
You now have to format your input which I am assuming is a time object or it is a string in the same format as the data in the database:
$yourdate = strftime("%Y-%m-%d", $input);
then construct your query
query = "select * from table where substring(datecol, 1, 10) = '$yourdate'";
execute this and you should be good
Based on the format that you're storing the date as a string, use the STR_TO_DATE function to parse out the date. Then you can use it as a part of the where clause to query desired data.
try this
select * from table where date(your_field)=CURDATE()
or specific date
select * from table where date(your_field)=DATE_FORMAT(2011-05-31 00:02:00, '%Y-%m-%d')
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.