My datetime format on my SQL Table looks like this
Table1:
Col1
FromDate
--------------------------
10 August 2017 - 02:10 pm
Col2
ToDate
--------------------------
10 August 2017 - 08:00 pm
What would be the applicable SQL SELECT query and or php code to check if ..
$submitFrdate = "10 August 2017 - 3:00PM";
$submitTodate = "10 August 2017 - 6:00PM";
will be compared to the table using it's DATE only if any of the dates from either the first or second variable collides to the dates of any of the 2 table columns? I understand that I can trim the time stamp using substr() to make it into just 10 August 2017 but the data on the table includes the time. Other than that, I am not sure whats the easiest way to make sure either of the submitted data doesn't hit either of the column's dates REGARDLESS of the time.
Thanks in advance.
MySQL has a function called STR_TO_DATE() which can reverse-format a date string into MySQL's native date format. Then you can do comparisons in the SQL.
Perhaps better solution is to use the DATETIME type for the column. Then you can directly do comparisons with them.
Related
Is there anybody who write query for time range. I am searching from last 6 days when I get the result it will all about dates. Where are times?
I need a solution for searching e.g. from 12:00:00 PM to 04:00:00 PM in datetime field. So how can I search that from mysql table on whatever dates the have. But the result will how on that time range. Any body have solution for that.
Use the TIME() function, it extract the time portion from a datetime field https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_time
SELECT TIME(datetime_col) FROM table
WHERE TIME(datetime_col) BETWEEN '12:00:00' AND '16:00:00'
I have a date that sometimes can be only a year, years and month or a full date. What is the best practice to save it on the db?
I need to do search later as: Give me all date between 2004 - 2008 or give me all date between 2004-05-21 to 2005-12-31
I would suggest to use DATE format (https://dev.mysql.com/doc/refman/5.5/en/datetime.html) and then if needed use more specific functions to query records by date ranges like
WHERE YEAR(created) BETWEEN 2010 AND 2011
etc.
More date\time related functions here:
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
Date 1 :- 2014-09-27 10:00:00
Date 2 :- 2014-09-29 11:00:00
This range is stored inside the database and it means that user has given time range for 2 days from 27 to 29 and timing from 27 -> 10:00:00 to 29 -> 11:00:00. This means that user will be available from 10 AM to 11 AM between 27 to 29, 2014.
Now if i pass 2014-09-28 13:00:00 which is in date range and also in time range because user specified the entire day for it as can be seen in the range.
SELECT * FROM TABLE_NAME WHERE Id = $Id AND DATE('$currentDate') BETWEEN DATE(From_DateTime) AND DATE(To_DateTime) AND TIME('$timeHour') BETWEEN TIME(From_DateTime) AND TIME(To_DateTime)
From_DateTime = Date1
To_DateTime = Date2
currentDate = 2014-09-28
timeHour = 13:00:00
Now the problem is that logically the parameter passed are within the range but using the query its not because in TIME its not checking the date, 13 is not between 10 & 11 so its not working. I have tried the DATETIME as well but its not working as giving me error.
I need a way to match date & time both at the same time. Anyone having any suggestion. I am using PHP as programming language.
If the values are stored in DATETIME like this format you mentioned
Date 1 :- 2014-09-27 10:00:00
Date 2 :- 2014-09-29 11:00:00
Then you don't even need all that complexity. Just use normal comparison operators
SELECT * FROM yourTABLE
WHERE startDate >= '2014-09-27 13:00:00'
AND endDate <= '2014-09-29 10:00:00'
Ofcourse you can use your PHP variables instead of the test dates I have there in the query. You can format your PHP values to be in line with MySQL date time format.
If you're expecting large result or large database, I would suggest you use the "BETWEEN".
But I would like to clarify your database schema has 2 fields to compare? Namely "To_DateTime" and "From_DateTime"? If so, I seconded Hanky's answer.
I am using the query
$query=mysql_query("select * from tablename
where name='$name'
and date between '$newDate' and '$newDate1'");
Suppose, I am giving username xyz and want to see data from 01st may 2014 to 02nd may 2014, it is returning data from 01st may to 02nd may along with 01st apr to 02nd apr too. As I have started adding data from 1st of apr. Same case is for all dates. It is showing data of particular date along with the data of other months having the same date. How to solve it. Need your help badly? Plz do reply my question! Thanx in advance?
You are using varchar(100) for date datatype. use DATE(datatype) then check. Because date function check the date in between Y/m/d format.
Very bad design, you should store date as date or datetime.
Now varchar is not considered as date and hence the comparison fails and it will be
treated as string comparison not date.
This is one of the way you can achieve what you are trying
select
name,
date_format(str_to_date(date,'%d/%m/%Y'),'%Y-%m-%d') from test
where
date_format(str_to_date(date,'%d/%m/%Y'),'%Y-%m-%d')
between
date_format(str_to_date('01/05/14','%d/%m/%y'),'%Y-%m-%d')
AND
date_format(str_to_date('02/05/14','%d/%m/%y'),'%Y-%m-%d')
DEMO
I have a table in SQL Server 2005, and it has column period (datetime).
The value of the column period is 8/7/2009 12:00:00 AM, when I query using php script it give me output Aug 7 2009 12:00AM.
Why it's not 8/7/2009 12:00:00 AM ???
Thank you
Your question reads like this:
The value of the column period is
{ a date, year 2009 month August day 7th at 12:00 AM}
When I query using php script it give me output "Aug 7 2009 12:00AM", i.e. one representation of the date.
Why it's not "8/7/2009 12:00:00 AM", i.e. another representation of the date
A datetime represents a point in time. It is a specific concept. You are comparing two separate representations of the datetime, or in other words, two ways of formatting the datetime value as a textual string.
The issue you are trying to solve is then, "How do I format a datetime as a string in PHP"? For which the answer would be to refer to the date_format (aka DateTime::format) function
To wit, the display specifier for 8/7/2009 12:00:00 AM is n/j/Y h:i:s A.