I have a table with a column DateFrom (Varchar) that uses format YYYY-MM-DDTHH:MM:SS
to get the time for a specific date I use this sql:
SELECT SUBSTRING(DateFrom FROM 12 FOR 5) AS From FROM calendar WHERE SUBSTRING(DateFrom FROM 1 FOR 10) = "'.$date.'"
this gives me:
HH:MM
.$date
(from datepicker) has format DD.MM.YYYY
I'm creating a website that should let the user see times associated with a date
can I convert the string from my database before checking if it's equal to .$date?
Or would it be better to change format from datepicker to YYYY.MM.DD instead?
How about getting rid of the the substring stuff and using this instead?
SELECT TIME(DateFrom) AS `From`
FROM Calendar
WHERE DATE(DateFrom) = STR_TO_DATE($date,'%d.%m.%Y')
This will work because your DateFrom field happens to be stored in the standard string format of a DATETIME object.
From is in backticks because it's a reserved word.
You really should consider changing the data type of your DateFrom column to DATETIME, and then indexing that column. Then you can change your WHERE clause to this
WHERE DateFrom >= STR_TO_DATE($date,'%d.%m.%Y')
AND DateFrom < STR_TO_DATE($date,'%d.%m.%Y') + INTERVAL 1 DAY
and you will get a very great performance advantage.
This is how i finally solved it:
$date1 = 18.02.2014;
$date2 = 22.02.2014;
$a = explode('.',$date1);
$revdate1 = $a[2].'-'.$a[1].'-'.$a[0];
$newdate1 = str_replace(".","-",$revdate1);
$b = explode('.',$date2);
$revdate2 = $b[2].'-'.$b[1].'-'.$b[0];
$newdate2 = str_replace(".","-",$revdate2);
SELECT SUBSTRING(DateFrom FROM 1 FOR 10) AS Date,
SUBSTRING(DateFrom FROM 12 FOR 5) AS StartTime,
SUBSTRING(DateTo FROM 12 FOR 5) AS EndTime
FROM Calendar WHERE SUBSTRING(DateFrom FROM 1 FOR 10)
BETWEEN "'.$newdate1.'" AND "'.$newdate2.'"
I know DATETIME would be better but that was not an option here (not my database).
So this solution takes a date with format dd.mm.yyyy and reformats it to yyyy-mm-dd and checks it against the substring date from my table.
so the query prints all:
Date, StartTime, EndTime
between two dates
Related
I have a table containing a field "created_at" whose Data Type is timestamp. I donot want to change it to DATE.
Can i query this table to fetch all rows of a day in format dd-mm-yyyy.
Note:
One approach I tried is:
a) Take Date in yyyy-mm-dd concatenate with 00:00:00
b) Take next date in yyyy-mm-dd concatenate with 00:00:00
and use below query to get all records of that day:
SELECT *
FROM news
WHERE created>='2016-12-13 00:00:00'AND
created < '2016-12-14 00:00:00'
Is this a good solution to my problem. Any better approach for this problem.
You can use the MySQL cast() function.
SELECT
*
FROM news
WHERE CAST(created_at AS DATE) = '2016-12-13'
This will discard the time component of your timestamp and do the comparison on only the date.
Reference: http://dev.mysql.com/doc/refman/5.7/en/cast-functions.html
If you don't want to change column datatype then you can go with this code
$startDate = strtotime( '2016-12-13' ); // it will convert date to timestamp format.
$endDate = strtotime( '2016-12-15');
$query = "SELECT * FROM news WHERE created >= '$startDate' AND created < '$endDate'";
This question already has answers here:
Select data between a date/time range
(8 answers)
Closed 1 year ago.
I want to search records between two dates in CodeIgniter I have tried many methods but not getting required result.
My Model
function search($date1 , $date2 ){
$this->db->where('date<',$date1);
$this->db->where('date >',$date2);
$result = $this->db->get();
return $result;
}
My controller
function getsearch(){
$date1 = $this->input->post('txtdate1'); // 02-06-2015
$date2 = $this->input->post('txtdate2'); // 19-06-2015
$data['result'] = $this->result_model->search($date1,$date2);
$this->load->view("search_view",$data);
}
Now I want all rows between 2 to 19 but I am getting nothing.
Note: date type in mysql is varchar
02-06-2015 is a really suboptimal way to store dates in SQL, because they don't collate sensibly. If you can switch to a DATE or DATETIME data type, you'll be able to do cool stuff like indexing on the date column. But that's not what you asked.
Also, I wonder if you have your inequalities wrong? What's the start and end of the range of dates you want to search?
To answer your specific question:
The DATE_FORMAT() MySQL function will convert your char dates to collatable dates. In particular,
DATE_FORMAT('02-06-2015', '%d-%m-%Y')
gets you a DATE object from your dates.
So this sort of php code will work
$this->db->where("DATE_FORMAT(date, '%d-%m-%Y') < ","DATE_FORMAT('$date1', '%d-%m-%Y')");
$this->db->where("DATE_FORMAT(date, '%d-%m-%Y') > ","DATE_FORMAT('$date2', '%d-%m-%Y'):);
If $date2 is '01-06-2015' and $date1 is '04-06-2015' this will give back the records that fall on the second and third days of June.
You may want this instead for a [$date1,$date2] range.
$this->db->where("DATE_FORMAT(date, '%d-%m-%Y') >= ","DATE_FORMAT('$date1', '%d-%m-%Y')");
$this->db->where("DATE_FORMAT(date, '%d-%m-%Y') < ","DATE_FORMAT('$date2', '%d-%m-%Y') + INTERVAL 1 DAY");
Notice your date column is wrapped up in a function. That defeats the use of any index to do the table search on dates. If your date column had a DATE datatype, this would work. If you had an index on date MySQL would use a highly efficient range scan.
$this->db->where("DATE_FORMAT(date, '%d-%m-%Y') >= ","'$date1'");
$this->db->where("DATE_FORMAT(date, '%d-%m-%Y') < ","'$date2' + INTERVAL 1 DAY");
In raw SQL this would come out to...
WHERE DATE_FORMAT(date, '%d-%m-%Y') >= '$date1'
AND DATE_FORMAT(date, '%d-%m-%Y') < '$date2' + INTERVAL 1 DAY
I would very much appreciate your help.
I have a mysql db that contains a Datetime field. In that field I have a particular date and time,
for example:
2013-10-03 22:28
I then have a PHP script that gets a particular date and time from the $_GET command, values separately: year, month, day, hour, minute.
From this GET I created a Date as follows:
$datum = $year."-".$month."-".$day." ".$hour.":".$minute.":".$seconds;
$date = date('Y-m-d H:i',strtotime($datum));
What I need to do now is somehow compare this new date I created with the last date value in the database (the most recent). The point is that after I compare this I want to check whether the last value in the db is older than 5 minutes and only then do a particular action (insert new row), and if the last date in the db is newer than 5 minutes, do nothing.
Use "select max(datetime_field) from tblname" to get the most recent value for the Datetime field.
$now = new DateTime();
$dateFromDB = new DateTime($someValueFromYourDataBase);
// subtract 5 minutes from now and compare with the stored timestamp
if ($now->sub(new DateInterval('PT5i') > $dateFromDB) {
// database timestamp is older - do something
}
you can use SQL like this:
select count(*) from `table` where `datefield` >= '$date'
$date can be calculated like
$date = date('Y-m-d H:i', strtotime($datum) - 5*60);
5*60 - it is 5 minutes
for performance reason I suggest you to add index to datefield and change select like:
select `datefield` from `table` where `datefield` >= '$date' limit 1
I'm working with a table and there is field in my table which stores raw time() function value as date.
I want to get rows with today date from this table .
So i figure out when time() func returns a 10 digit number like 1316352184 the first 5 digits are for year , month , day which i need for getting today's date and the rest is for hour minute Second which i dont need
So i get today without hour and... like
$t = time();
$t = $t /100000;
$today =(int)$t;
Now i need to get rows with today date from the table but i'm not sure how to do that.
How can i get first 5 digits of stored date in database in my query to compare it with $date?
Something like this:
$sql = "SELECT * FROM TABLE WHERE ((int)date/100000) as date = $today ;
select * from table
where from_unixtime(unix_timestamp_field,'%Y-%m-%d') = curdate()
Why you don't use:
$sql = "SELECT * FROM TABLE WHERE date(date) = date(NOW());
What you have is a UNIX timestamp. The number of seconds since January 1st, 1970.
You can use date() and mktime() to work out what todays timestamp is, then do date > the timestamp. If that make sense.
Sounds like you should use the DATETIME or TIMESTAMP data type for your column so you can use MySQL's date functions.
Would this be possible? I've used this to insert the date into a field called "date":
$date=date("m/d/y");
$sql="INSERT INTO pool (date) VALUES('$date' )";
$result=mysql_query($sql);
I've used this statement to get the date a week ago:
$variable = date('d-m-y', strtotime('-1 week'));
So how would I SELECT any rows which were added last week?
Instead of storing your dates as m/d/y, you should store them as Y-m-d :
$date=date("Y-m-d");
$sql="INSERT INTO pool (date) VALUES('$date' )";
In the database, you dates will then look like 2011-04-09.
That format is much easier to work with : alphabetical comparisons will work.
Which means that searching for rows that are older than a certain date would become something like this :
$variable = date('Y-m-d', strtotime('-1 week'));
$query = "select * from pool where date < '$variable'";
Also note that instead of working with a date field which is a varchar (or an equivalent) in your database, you could use a DATE column -- which would allow to to work with date and time functions in MySQL.
If the date field is a proper date type you can do < or > in your sql query. For example -
SELECT * FROM table WHERE date > '$date'
If you want everything from 1 week ago to now you can do something like the above or
SELECT * FROM table WHERE date BETWEEN '$date' AND NOW()