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')."'";
Related
I sent parameter date to check if that date between two date from database.
example date string parameter that i sent to API is "09/01/2020" which format is mm/dd/YYYY
this format also same for date string in database table column.
Now in database, I put date_start as "08/21/2020" and date_end as "09/05/2020"
My code is :
$date = $data["date"];
to get date string parameter that I sent to API.
Example table name is promo,
my code is :
$sql = "SELECT * FROM promo WHERE STR_TO_DATE('" . $date . "', '%M%M/%d%d/%Y%Y%Y%Y')
between date_start and date_end";
But I did not get it.
How to correct that to get it?
Edited my latest code is like this but still did not get it:
$date = $data["date"];
$date = DateTime::createFromFormat('m/d/Y', $date);
$date = $date->format('m/d/Y');
$sql = "SELECT * FROM promo WHERE $date BETWEEN STR_TO_DATE(date_start, '%m/%d/%Y') AND
STR_TO_DATE(date_end, '%m/%d/%Y')";
You said:
Now in database, I put date_start as "08/21/2020" and date_end as "09/05/2020"
This is not best practice, and you should always store your dates as a proper date or datetime type in MySQL. Had you done this, you would only need a range comparison in your query:
SELECT *
FROM promo
WHERE ? BETWEEN date_start and date_end;
If you must stick with your current design, then you would actually have to use STR_TO_DATE on the start and end dates:
SELECT *
FROM promo
WHERE ? BETWEEN STR_TO_DATE(date_start, '%m/%d/%Y') AND
STR_TO_DATE(date_end, '%m/%d/%Y');
To the ? placeholder above, you should bind either a valid PHP date, or a valid MySQL date literal string. But again, I strongly recommend fixing the design problem.
I am trying to do, what I assume is, an easy task of adding days to a date.
I have a date stored in a MySQL table, in a column called meta_date, with the type of DATE (A date, supported range is 1000-01-01 to 9999-12-31)
I retrieve this date from the database as follows:
$thisId = 1;
$dateQuery = mysqli_query($connection, "SELECT * FROM `sometable` WHERE `id` = '$thisId'");
$fetchDate = mysqli_fetch_assoc($dateQuery);
$theDate = $fetchDate['meta_date'];
Now I add a number of days to this date.
$newDate = date("Y-m-d", strtotime($theDate . " + 7 days"));
Next I put it back inside the database with an UPDATE query.
$editDate = mysqli_query($connection, "UPDATE `sometable` SET `meta_date` = '$newDate' WHERE `id` = '$thisId'");
However the date always returns as 0000-00-00 after the update.
Am I missing something here to do with the way the date is handled in PHP?
edit: The data I first retrieve from the database (into $theDate) is "2016-11-30".
You can use Mysql's built in function DATE_ADD()
Syntext
DATE_ADD(date,INTERVAL expr type) Where date is a valid date expression and expr is the number of interval you want to add.
For your case
UPDATE sometable
SET `meta_date` = DATE_ADD(`meta_date` , INTERVAL 7 DAY)
WHERE `id` = '$thisId';
I am trying to select an unixtimestamp column from my database, as readable date time:
SELECT count(*) FROM users WHERE user_by="Admin" AND expire(FROM_UNIXTIME(unixtime),'%Y-%m-%d')='2015-10-02'
Above gives me this error:
#1305 - FUNCTION database_maindb.expire does not exist
How can I select the unixtimestamp from column expire as datetime, in the format: year-month-date?
assuming your database field is called "expire":
SELECT count(*) FROM users WHERE user_by="Admin" AND FROM_UNIXTIME(expire,'%Y-%m-%d')='2015-10-02'
see http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_from-unixtime for details
You don't need to select is as the YMD date stamp, you could do something such as (using your tags)
$query = "SELECT * FROM `table`;";
$result = pdoObject->queryMethod($query);
$result= pdoObject->fetchAssocMethod($result);
$date = date("Y-m-d", $result['epochCol']);
I hope this helps.
I'm trying to create a SQL query that finds results from this day in history (ie today's date, not including the year)
I get the current date in PHP:
$today = date("Y-m-d", time());
I get no errors but also no results are returned when I use $today in this query:
SELECT name, date FROM births WHERE MONTH(date) = MONTH($today) AND DAY(date) = DAY($today)
There are two rows in the date column that contain today's date in Y-m-d format. I'm not sure what's going on here. Is it because of an incompatible use of the variable? This is what my table structure looks like:
dates in mysql should be surrounded in single quotes so this is what you are after
$today = date("Y-m-d", time());
$sql = "SELECT name, date FROM births WHERE MONTH(date) = MONTH(`{$today}`) AND DAY(date) = DAY('{$today}')";
$query = mysqli_query($con, $sql);
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.