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
Related
i'm working on codeigniter and mysql db. I have run in to a issue where i need to show recent data as per date.
i have table name tbl_tournament where i'm going to fetch tournament data whose tournament_end_date >= $todays_date
My Query
$today = date("d/m/Y");
$this->db->select('*');
$this->db->from('tbl_tournament');
$this->db->where('is_deleted', FALSE);
$this->db->where('tournament_end_date_time >=', $today);
$this->db->order_by('tournament_start_date_time');
$query = $this->db->get();
return $query->result_array();
the output of this query is empty array
array();
When i manually change tournament_end_date_time = 01/04/2018 in database and query $this->db->where('tournament_end_date_time >=', '01/04/2017'); i get results. But when i change date in query as $this->db->where('tournament_end_date_time >=', '31/12/2017'); i get empty array.
In Mysql database i have used varchar as the data type tournament_end_date_time
Thank in Advance.
You store datetimes as strings (with VARCHAR type), hence they're compared as strings - character by character. For obvious reasons, '3' character is greater than '1'. That's why with filter set...
WHERE tournament_end_date_time >= '31/12/2017'
... you'll only get the results where corresponding values start from '31/12' - in other words, of December, 31 (of 2017 - or any year after it).
To solve the problem, you can use STR_TO_DATE() MySQL function to convert the existing column value to an actual date. Note that the param will be treated as a date literal as it follows 'YYYY-MM-DD' format:
WHERE STR_TO_DATE(tournament_end_date_time, '%d/%m/%Y') >= '2017-12-31'
... or, in PHP syntax, following CodeIgniter conventions:
$this->db->where(array(
"STR_TO_DATE(tournament_end_date_time, '%d/%m/%Y') >=" => date("Y-m-d")
));
A better choice is doing a single-time update operation on this column to convert all its values to DATE type at once.
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
In database I am storing date value unix timestamp value for e.g.'1434952110' using time() now I am trying to compare todays value with this value as below
$jobpostdate = date("Y-m-d");//today's date passing in database to compare
query
$sql = "SELECT jsp_title, jsp_subtitle, jsp_desc, jsp_location, jsp_date ";
$sql .= "FROM js_projects WHERE DATE(jsp_date) = '$jobpostdate' ";
I tried above query , but even if the value is present I am getting no rows found, where i am going wrong how can I compare date values ? I know how to compare in php but i don't know exactly how to check in query ,Pls any one can help
If I understand correctly you have a unix timestamp in a varchar field and you can't change this.
If you compare the unix timestamp directly you will only get results that match the exact second of the timestamp.
You can use FROM_UNIXTIME() to convert the timestamp in a date value you can actually use:
WHERE DATE(FROM_UNIXTIME(jsp_date))=CURDATE()
This compares the date portion of the timestamp with the current date, giving you all database entries of the given day.
Note: This could create performance problems, since a lot of conversions occur on every request. You should really convert the unix timestamp in the database into a DateTime or Date field.
You could use PHP DateTime class.
$today = new DateTime('now'); // Can be what ever you want, not only now
// see below for reference.
$jobpostdate = $today->getTimestamp();
and query:
$sql = "SELECT jsp_title, jsp_subtitle, jsp_desc, jsp_location, jsp_date ";
$sql .= "FROM js_projects WHERE jsp_date = '$jobpostdate' ";
!PS I can't test it, but in a nutshell it should work.
Fore more reference about this awesome class go here:
http://php.net/manual/en/class.datetime.php
Basically i'm doing a simple wedding planner and i am trying to check whether the date that the user has inserted as available or whether the venue is booked. Basically, it's falling over with my SQL query. Initially i am setting the time:
$time = mktime(0, 0, 0, $month, $day, $year);
Then i have an sql query where i am checking it against an id variable that is previously set and the date that is passed through.
$sql2 = "SELECT * FROM venue_booking WHERE date_booked = ".$time." AND venue_id =".$id;
In this case the date in the database is 2015-01-01 and the date i'm passing through is 2015-01-01, I am checking this in an if statement if the amount of rows returned from the database is greater than 0 then echo booked, else echo available.
Even if the output is meant to say booked it still says available. Is there an issue with the way i am checking the time against mysql's date.
date_booked - This is a MySQL date (2014-01-01)
Anybody have any ideas?
Your current query is missing quotes around your date string so it wouldn't work as it is.
But to answer your question, just pass a valid date string in YYYY-MM-DD format and your query would work:
$date = $_POST['date'];
// Put date validation code here. I.e. make sure it is in YYYY-MM-DD
// format, etc. Might as well escape it, too since you aren't using
// prepared statements.
$sql2 = "SELECT * FROM venue_booking WHERE date_booked = '".$date."' AND venue_id =".$id;
I should also mention that you should probably switch to using prepared statements as it will make using user-provided data in queries safer.
Here is a possibly useful example of date validation. If you need to convert the date from one format to another, this will show you how.
mktime() returns a PHP timestamp, which is a unix timestamp, which is "seconds since the epoch", aka Jan 1/1970. You cannot directly compare that against a mysql date field. You'd literally be doing
WHERE '2014-01-01' = 12345678
Try
$ts = date('Y-m-d h:i:s', mktime(0,0,0,$month, $day, $year));
$sql = "SELECT ... date_booked = '$ts'";
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