Trying to find a record between two dates - php

My Queries looks like this
WHERE `Project`.`user_id` = 9
AND `Project`.`project_status` = 1
AND `Project`.`project_type_id` = 4
AND `Project`.`approval_date` >= '01/08/2012'
AND `Project`.`approval_date` <= '01/12/2012'
I do have a record that matches this criteria, I actually created the query based on one of the records but it does not return. I really do not want to use the BETWEEN because the application can either use start date or end date

You should convert '01/08/2012' and '01/12/2012' to MySQL date/time values using STR_TO_DATE()
e.g.
STR_TO_DATE('01/08/2012', '%d/%m/%Y')

Well that's really only part of the query -- any, "between" works differently than you imply - here's an excerpt from the MySQL manual:
•expr BETWEEN min AND max If expr is greater than or equal to min and
expr is less than or equal to max, BETWEEN returns 1, otherwise it
returns 0. This is equivalent to the expression (min <= expr AND expr
<= max) if all the arguments are of the same type. Otherwise type
conversion takes place according to the rules described in Section
11.2, “Type Conversion in Expression Evaluation”, but applied to all the three arguments.
All that aside what is the datatype of the "approval_date" column?

This looks as if it should work on first glance, however the values you have supplied are not valid datetimes. Try this instead
WHERE `Project`.`user_id` = 9
AND `Project`.`project_status` = 1
AND `Project`.`project_type_id` = 4
AND `Project`.`approval_date` >= '2012-01-08 00:00:00'
AND `Project`.`approval_date` <= '2012-01-12 00:00:00'

you can still use BETWEEN
WHERE `Project`.`user_id` = 9
AND `Project`.`project_status` = 1
AND `Project`.`project_type_id` = 4
AND (`Project`.`approval_date` BETWEEN '2012-01-08 00:00:00' AND '2012-01-12 23:59:59')

My main problem was getting the string to format into datetime. kissmyface was correct on just structuring it correctly so mysql could understand. So I used php to convert it before sending it out.
"Project.approval_date >= '".date("Y-m-d H:i:s", strtotime($start))."'";
"Project.approval_date <= '".date("Y-m-d H:i:s",strtotime($end))."'";
this was pretty much my solution. unless anyone knows away to do the exact same thing on mysql side.

Related

SQL query error using to_char

The last line of my query will not run without "not a valid month" error. I am trying to convert a start time in GMT to local time using an offset in one of the columns, then compare this to a date I have in a variable. It works without using the to_char and offset.
Any insight would be appreciated.
AND to_char(CT.START_GMT + K.OFFSET/1440,'MM/DD/YYYY HH24:MM:SS') >= TO_DATE('$From_Date', 'mm/dd/yyyy')
AND CT.START_GMT + K.OFFSET/1440 >= TO_DATE('$From_Date', 'mm/dd/yyyy')
You don't need a TO_CHAR() conversion.. if CT.START_GMT is a DATE
If CT.START_GMT is not DATE/TIMESTAMP , then you need to do TO_DATE() with the right format.

Select query with month and year condition

I have two query conditions here, and I would think that these queries would result in the same data, but they don't.
// 1st Condition:
WHERE month(jurnal.time) >= '01'
AND month(jurnal.time) <= '06'
AND year(jurnal.time) = '2012'
// 2nd Condition:
WHERE jurnal.time > '2012-01-01'
AND jurnal.time <= '2012-06-30'
I want the first condition to result in the same data as the second conditions.
Thanks in advance.
Your first version includes jurnal.time = '2012-01-01', whereas your second condition does not (owing to the use of > rather than >=).
It isn't clear from your question what is the data type of the jurnal.time column:
if it's DATETIME or TIMESTAMP, your second example will not have included any time after 00:00:00 on June 30 so one must instead either take only the date part:
WHERE DATE(jurnal.time) BETWEEN '2012-01-01' AND '2012-06-30'
or else ensure that the comparisons include all times of the day:
WHERE jurnal.time BETWEEN '2012-01-01 00:00:00' AND '2012-06-30 23:59:59'
if it's DATE, this point will not have affected your examples but one can nevertheless simplify your second example to:
WHERE jurnal.time BETWEEN '2012-01-01' AND '2012-06-30'
I recommend the BETWEEN comparison operator with CASTs:
WHERE jurnal.time BETWEEN CAST('2012-01-01' AS DATE) AND CAST ('2012-06-30' AS DATE)
Be sure to cast the stings to the same type as the column of jurnal.time.
Your conditonals in the first query are trying to compare as if they were numbers the values returned from the date parsing functions, but i think they are just strings. '01' and '06' are interperted by sql as plain strings, not the numbers 1 and 6.
The second is sql is smart enough to convert the full date string representation into a date to use in the comparison.

Date search Sql Query

im storing the timestamp in mysql database in (INT) column, And i want to search the rows with between the dates. Anyone would please help what should be the Sql query to find the rows between two dates?
dates are entered like
FROM DATE = 15-10-2011
END DATE = 01-11-2011
It depends on what algorithm you use to convert the date strings to int values.
If the algoritm is mototonic, for example: If a day (say 15-10-2011) is converted to n (say 5037), then the next day (16-10-2011) is always converted to n+1 (so 5038 in this example.)
then you could just use:
WHERE IntField BETWEEN MySpecialConvertDateToIntFunction('15-10-2011')
AND MySpecialConvertDateToIntFunction('01-11-2011')
If your field stores different timsetamps as different integers (and the conversion is monotonic), you could change the above code slightly to:
WHERE IntField >= MySpecial...Function('15-10-2011')
AND IntField < MySpecial...Function('02-11-2011') --- notice the date+1
But it's usually better to use a field of the MySQL DATE type for storing dates. Unless you want to store dates before 1000 or after 9999 off course.
If you want to store timestamps, there's also a TIMESTAMP type. Read the
MySQL docs: DATETIME, DATE, and TIMESTAMP Types
You can use The BETWEEN operator, which selects a range of data between two values. The values can be numbers, text, or dates.
You can see there:
http://w3schools.com/sql/sql_between.asp
I would ask you to set data type as timestamp/datestamp & then
//php code
$date1=date ('Y-m-d' , strtotime ( "15-10-2011") );
$date2=date ('Y-m-d' , strtotime ( "01-11-2011") );
//sql code
SELECT * FROM tbl_mytbl WHERE DATE(attr_date) <'$date2' AND DATE(attr_date) >'$date1'
Can you use the mysql FROM_UNIXTIME function dev.mysql.com - function from_unixtime
SELECT *
FROM 'table'
WHERE FROM_UNIXTIME(intTimestamp)
BETWEEN
date ('Y-m-d' , strtotime ( '15-10-2011') )
AND ('Y-m-d' , strtotime ( '01-11-2011'));
I had made a mistake with the date input but have fixed.

Query for events with a date and time greater than now

all - fairly simple query question that's been hounding me: How do I query for entries with a date and time only greater than now, or when the query is run (page requested)?
I've seen some examples but they aren't good enough for me to modify. Here's my code:
$todaysDate = date("Y-m-d h:i:s");
$params = array('select'=>'*', 'limit'=>3, 'orderby'=>'t.event_StartDate ASC', 't.event_StartDate < "$todaysDate"' );
An example of the variable "t.event_StartDate" outputs "2010-12-10 22:18:42" so I assume that may be how it's saved in the database.
I suspect I'm not building the date correctly or I need to be using a time function I'm not familiar with in MySQL. Help? This is for outputting a series of events with date and time.
I think you reverse the use <, it should be >
try
SELECT ... WHERE t.event_StartDate>NOW();
/* you don't even need to set $todaysDate */
t.event_StartDate > NOW() don't will returns nothing? a date higher of now lol. I'm confused.

query to find a partial match

I want to write a query to find all the items in my 'schedule_items' table that match a YYYY-MM-DD value of a DATETIME field.
something like: SELECT * FROMschedule_itemsWHEREstart?matches? '$date'
Where start would be a DATETIME field with data like 2009-09-23 11:34:00 and $date would be something like 2009-09-23
Can I do this or do I need to load all the values and then explode and compare the values in my DATETIME start field?
You can use LIKE in MySQL
SELECT * FROM schedule_items WHERE start LIKE 'some-date%'
You put a % sign as a wildcard.
Be aware though that like can be a very heavy query. Specially if you use % at the start of a string.
If you are working with dates, like it seems you are saying :
Where start would be a DATETIME field
with data like 2009-09-23 11:34:00 and
$date would be something like
2009-09-23
Using LIKE might not be the only/best solution : there are functions that deal with dates ; and you probably can use comparison operators too.
In your case, you can probably use something like this :
select *
from headers_sites
where date_fetch >= '2009-07-15'
limit 0, 10
Of course, you'll have to adapt this query to your tables/fields ; something like this might do, I suppose :
SELECT * FROM schedule_items WHERE start >= '$date'
This will get you every data for which the date is more recent than $date.
If you only want the date of one day, this could do :
SELECT *
FROM schedule_items
WHERE start >= '$date'
and start < adddate('$date', interval 1 day)
This might be better than "like", if you start column has an index -- not sure, though ; but, still, what you are willing to get will be obvious from your query... And that is nice.
in SQL you can use LIKE instead of = and then you get the % wildcard. Example:
select ... where start like "2009-09-%";
(That would match anything from August 2009)
If it is needed to extract records of a date from datetime field, you need to use following format:
SELECT * FROM schedule_items WHERE date(start) = 'some-date time'

Categories