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.
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 data inserted into table 'dnt' with colum date being php datetime.
$date = time();// this was inserted into the db as :1481811673
$today = time();
"SELECT * FROM `dnt` WHERE 'date' = '$today'";
You cannot compare a timestamp with date today as timestamp changes per second so to compare right, you need to convert the timestamp stored in db into a dateformat and then compare that date with today date. You can do it as follows:
$today = date('Y-m-d'); // date today in format - YYYY-mm-dd
//your query
"SELECT * FROM `dnt` WHERE DATE_FORMAT(FROM_UNIXTIME(dnt.date), '%Y-%m-%d') = '$today'";
I hope it helps
This appears to answer your question http://www.tomjepson.co.uk/mysql-select-from-table-where-date-today/
tldr;
SELECT * FROM myTable WHERE DATE(myDate) = DATE(NOW())
Short and simple:
$stmt = "SELECT * FROM `dnt` WHERE 'date' = '".date('Y-m-d')."'";
To work with a timestamp:
$now = new DateTime();
$stmt = "SELECT *
FROM table
WHERE date = '".$now->getTimestamp()."'";
Detail
What the above query does is to SELECT all (*) FROM table dnt WHERE date =
the date() function in PHP returns a certain date based on the parameters you put in. the Y is for the years, the m for the months and the d for the days. So it will become date('Y-m-d') which will return 2010-01-01 for example.
the '". and ."' are to escape the php function so that it will not give you any syntax errors.
Besides what Peter said - the query is incorrect. You are comparing the date string value (date between single quotes) to a timestamp.
$stmt = "SELECT * FROM `dnt` WHERE `date` = '".date('Y-m-d')."'";
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.
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()
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');