I have the field datetime with DATETIME type in MySQL. In PHP script I set date begin and date end like this: 11/12/1999 and 11/12/2001. In my table datetime saved in the next format: 11.11.1888 00:00:00. How can I compare these dates?
Thanks.
It could be done more easly, but this is one way:
$begindate = strreplace($begindate,'/','.');
$enddate = strreplace($enddate,'/','.');
mysql_query("SELECT * FROM MyTable
WHERE dateField BETWEEN '$begindate 00:00:00' AND '$enddate 23:59:59'");
should be something like
SELECT *, STR_TO_DATE(datetime,'%d.%m.%Y') as newdatetime
FROM table
WHERE newdatetime >= STR_TO_DATE('11/12/1999','%d/%m/%Y')
AND newdatetime <= STR_TO_DATE('11/12/2001','%d/%m/%Y');
Related
I am using WPDB and here is my SQL
$date = date('d-m-Y');
$reservations = $wpdb->get_results( "SELECT * FROM reservation_db WHERE `date` > '$date'");
I need to select the date in my database when the date in database greater than today.
My date format is dd-mm-yyyy, but I think because it's save in text, it only compares days(dd) which is wrong, any solution to solve this?
MySQL offers a STR_TO_DATE function to convert a date string to date:
SELECT * FROM reservation_db WHERE STR_TO_DATE(`date`) > '$date'
But as ankit suthar mentioned in above comment, it is not recommended to store dates as text.
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'";
I stored expiry date field as var char datatype in MySQL table. Now i need to compare expiry date with current date. How to convert var char datatype as date in php coding and compare date in select query where condition.
why don't you do it in the query itself.
$current_date=date("Y-m-d");
mysql_query("select * from your_table where STR_TO_DATE(expiry_date,'%Y-%M-%D')>'".$date."');
or
You can do it in php itself
if(strtotime($current_date) > strtotime($row['date'])) {
//your code
}
This works for me:
SELECT * FROM table_name WHERE start_date <= CURDATE() AND end_date >= CURDATE()
Varchar date field is stored like this: 2013-06-17 00:00
$date = date("Y-m-d H:i");
$query = "SELECT Date,Away,vTotal,vML,Home,hLine,hTotal,hML FROM `LINES` ORDER by Date ASC WHERE Date > '$date' ";
First. Your query is wrong.
$query = "SELECT Date,Away,vTotal,vML,Home,hLine,hTotal,hML FROM `LINES` WHERE Date > '$date' ORDER by Date ASC";
Also you can compare string values in MySQL by more or less operators.
P.S. Think about conversion to date/time type.
http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_str-to-date
Believe you'll want to use the function above and be doing a Date comparison. Agree with the comment that dates should be stored as DATE DATETIME or TIMESTAMP.
I have row in which is string value from strtotime(), for example 1303448400.
My table has following stucture:
id | date
And from my input I recive date in this format: MM/DD/YY.
How to create a query in SQL which will select id where date is greater than 11/11/13?
You could convert your date (11/11/13) to a timestamp before using it in a query, using mktime():
http://php.net/manual/en/function.mktime.php
Use strtotime as you said,
SELECT * FROM TABLE WHERE DATE > strtotime($yourDate)
$date=strtotime('11/11/13');
SELECT * FROM TABLE db_date DATE > $date
This is how it will work for you.
$date_1 = 11/11/13; // your date
$date_2 = strtotime($date_1); //change it to strtotime
$query = mysql_query("SELECT id FROM table WHERE date DATE > '".$date_2."'"); //select id which is greater
Change the variables and rows to fit yours.