how to get data between two date with two different year? - php

My query below doesn't seem to work.
SELECT * FROM `test` WHERE date between '12/30/2013' and '01/05/2014'
But when I change the order of dates like in this query below, it seems to be working
SELECT * FROM `test` WHERE date between '01/01/2014' and '01/05/2014'
What is the correct way to use date ranges in SELECTs?

It won't work because the dates are not ISO 8601 formatted: "2013-12-30" for example.
The BETWEEN clause makes a string comparaison, so you need either to use a correct date format in your database, or format the dates with DATE_FORMAT() or STR_TO_DATE(str,format).
Edit:
Try this query, which will work if you store dates as strings formatted as %m/%d/%Y, which is a bad idead, MySQL has a built-in DATE format :
SELECT * FROM test where STR_TO_DATE(date, '%m/%d/%Y') between STR_TO_DATE('01/01/2014', '%m/%d/%Y') and STR_TO_DATE('01/05/2014','%m/%d/%Y');
The DATE_FORMAT MySQL takes a DATE or DATETIME value as first argument, which you don't use, that's why it didn't work (in addition to the '$' you used instead of '%' before 'Y')

Use php and sql together to get the result as below
$date1 = date("Y-m-d", strtotime('12/30/2013'));
$date2 = date("Y-m-d", strtotime('01/05/2014'));
$sql = "SELECT * FROM `test` WHERE date between '".$date1."'
and '".$date2."'";

select * from test where date between "2013-12-30" and "2014-01-05"
This will work fine

Related

I have a MySQL Date formatting that returns old records

So I'm a little befuddled. My client selects 10-01-2016 ($datefrom variable) and 10-02-2016 ($dateto variable). When executed, it will return the dates for 2015 and 2016.
Here is my php code:
$datefrom = $mysqli->real_escape_string($_GET['datefrom']);
$dateto = $mysqli->real_escape_string($_GET['dateto']);
$query = "SELECT * FROM tbl_Dates WHERE (DATE_FORMAT(date, '%m-%d-%Y') BETWEEN '$datefrom' AND '$dateto') AND date_type='1'";
When I run the following query in MySQL (just replacing the variables):
SELECT * FROM tbl_Dates WHERE (DATE_FORMAT(date, '%m-%d-%Y') BETWEEN '10-01-2016' AND '10-02-2016') AND date_type='1';
It runs and gives both 2015 and 2016 date records.
When I change the DATE_FORMAT lines and the variables to the following:
SELECT * FROM tbl_Dates WHERE (DATE_FORMAT(date, '%Y-%m-%d') BETWEEN '2016-10-01' AND '2016-10-02') AND date_type='1';
It runs fine, only giving 2016 date records.
the system is about a year old, LOL, hence why it is now giving the errors. How do I correct the SQL syntax to have it query correctly without having to change the php code down the line that uses the %d-%m-%Y formatting?
Thanks for any help you guys can provide,
Jim
Since your dates aren't in yyyy-mm-dd format, MySQL will be comparing them as STRINGS, not dates.
That means
10-23-2016 > 10-24-2015
is FALSE, because 3 is SMALLER than 4 in string comparisons. The fact that we humans know it's a date and know that 2016 comes after 2015 is irrelevant. MySQL isn't human, it isn't an AI, and when it sees strings, it compares them as strings.
If you want to do 'date math' in MySQL, then you MUST use the native formats.
Try This,
$datefrom = $mysqli->real_escape_string($_GET['datefrom']);
$dateto = $mysqli->real_escape_string($_GET['dateto']);
$query = "SELECT * FROM tbl_Dates WHERE date BETWEEN STR_TO_DATE('$datefrom','%m-%d-%Y') AND STR_TO_DATE('$dateto','%m-%d-%Y')) AND date_type='1'";
STR_TO_DATE will convert formatted string to date,
DATE_FORMAT() will convert formatted string representation.
Here is what worked:
$query = "SELECT * FROM tbl_Dates WHERE (DATE_FORMAT(date, '%Y-%m-%d') BETWEEN STR_TO_DATE('$datefrom', '%m-%d-%Y') AND STR_TO_DATE('$dateto', '%m-%d-%Y')) AND date_type='1'";

Compare oracle date in query with PHP

I want to compare dates retrieved from oracle database in php. but i don't know how to convert month name like Sep to SEP. I don't know how to convert month in capital letters. I want to convert 2015-09-01 formate to 03-SEP-15.
//sDate: 2015-09-01 eDate: 2015-09-03
$date1=date('d-M-Y', strtotime($sDate));
$date2=date('d-M-Y', strtotime($eDate));
// CREATEDATE: 03-SEP-15 02.44.42.000000 PM
QUERY:
$stid = oci_parse($conn, "SELECT * FROM table1 WHERE CAST(CREATEDATE AS DATE) between '".$sDate."' AND '".$eDate."'");
ERROR:
Warning: oci_execute(): ORA-01861: literal does not match format string
In Oracle you can convert a string into a date using the to_date() function. And a date to a string using the to_char() function
Example
to_date('01-JAN-2015','DD-MON-YYYY')
will return a value of type date.
Now, in your query you must make sure that the values in your "between" statement are date values. The only way to ensure that is to make $sDate a atring value and use the to_date function.
Assuming that your date strings are in format 01-JAN-2015...
$stid = oci_parse($conn,
"SELECT *
FROM table1
WHERE CAST(CREATEDATE AS DATE)
between to_date('".$sDate."','DD-MON-YYYY')
AND to_date('".$eDate."','DD-MON-YYYY')");
See also the Oracle documentation on date formats.
http://docs.oracle.com/cd/B28359_01/server.111/b28286/sql_elements004.htm
So you have a date string like this: '2015-09-01' and you want to convert it to '01-09-2015' to be used in a query.
If you want to debug if the issue is in how you handle the date conversion in php try to do:
list($year,$month,$day)=explode('-',$date);
$months = ['','JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC'];
$date = implode('-',$day,$months[$month],$year);
Then try to run the query. You don't need to take the string, convert it into a date (unix timestamp) and convert it back to a string if you just need to change the order of the elements in the date. You have to follow your approach if you want to manipulate your date adding or removing some time or stuffs like that.
If the query is still in error and you are sure that dates in your db are stored with that pattern than the issue is in the query syntax

SQL Query in php not working as required

I have a Database with column name DT, that has values:
1. 16-05-2015
2. 16-05-2015
3. 30-06-2015
4. 30-06-2015
Sql query in php that I am using is:
$sql = "Select * from logs WHERE (DT<= '$to') AND (DT>= '$from') ";
I have also used query:
$sql = "Select * from logs WHERE (DT BETWEEN '$to' AND '$from') ";
Both the queries are not functioning in the same way as required.
If $to=31-05-2015 and $ from = any date before 31st, the query displays the 30-06-2015 date as well, but it is not in the range.
When $to=30-05-2015, correct results are displayed but choosing any date after 30-05-2015 does not display the correct range, what could be the reason of it?
You have to change your date format to yyyy-mm-dd while saving it to the database and change it to the dd-mm-yyyy while displaying, and also change the datatype of the column to the datetime to make you query work
$newdate = date("d-m-Y", strtotime($yourdate));
apparently, DT is not configured as date in your DB.
And thus the values are ordered lexigoraphically, as strings.
(In lexigoraphical order: 30-... is before 31-..., no matter what ... might be)
You can either change your data to be saved as yyyy-mm-dd strings.
Or configure the column correctly as date

php convert time() to date format on the fly for sql

I have a while statement to echo some data to the user from specific dates.
I have stored the dates in my data base in this format: time() I want to write my sql statement in this format to check only the date date("m/d/y", some time). like so:
$page_visits_count = $mysqli->query("SELECT COUNT(`id`) FROM `visits` WHERE date('m/d/y', `time`) = '$date' ");
This part date('m/d/y', ``time``) is obviously wrong, but how would I be able to fix this problem ?
Note: I tried adding one day (3600 * 24) to my time. But that sometimes put me the middle of the next day.
If I understand correctly you're wanting to convert a UNIX timestamp (stored in DB column time) into a date format, then compare that date to the $date variable.
If that's true, FROM_UNIXTIME is what you're probalby after:
"SELECT COUNT(`id`) FROM `visits` WHERE FROM_UNIXTIME(`time`, '%m/%d/%Y') = '$date'");
You'll probably want to tweak the %m/%d/%Y part to match the format of your $date variable, see the FROM_UNIXTIME docs I linked to for a list on what formats are available.

How to use the mysql "LIKE" syntax with a timestamp?

I want to allow my users to search the database for a row that was submitted on a certain day. The date is entered into the field in the database using the date() function which is great, but when i use the php strtotime function, of course the dates are not exactly the same.
Is there some clever mysql function that can help me with this?
I had an issue with this before.
You're best to generate a start and end date then use the BETWEEN function instead.
So something like this:
$start_date = "2009-01-01 00:00:00";
$end_date = "2009-01-01 23:59:59";
$sql = "SELECT * FROM table WHERE date BETWEEN '$start_date' AND '$end_date' AND id = 'x';
Of course you would just pull your dates from the DB using strtotime and append the time stamps - depends how you used date()
Hope this helps :)
You can use MySQL's DATE() function to extract date part of the timestamp:
select ... from table ... where DATE(date_column) = '2010-01-25';
If you have problem submitting '2010-01-25' from PHP, you can use PHP's date function with 'Y-m-d' as parameter to only get the date part.
$d = date('Y-m-d', strtotime(...));
Looking at your question closely, it seems you'll need both of those. First use PHP's date function to get only the date part and then use MySQL's date to match only those records.
PHP:
$timestamp_from_php = strtotime('December 25, 2009');
SQL:
select
`fields`
from
Table t
where
date_format('Y-m-d', t.`datetime_field`) = date_format('Y-m-d', '$timestamp_from_php')

Categories