I am trying to update a MySQL table with current date into a column which type is DATE. current date structure is (2014-05-15). I've tried a few ways but it is not updating the date, however it is updating other fields.
MySQL query.
$q = "UPDATE TABLE SET
DESCRIPTION =".$db->qstr($string).",
DATE =".$db->qstr(date('Y-m-d')).",
LAST_ACT =".$db->qstr( time())."
WHERE ID=".$db->qstr( $id);
I've tried DATE = DATE(NOW()), and DATE = DATE(), but it is not updating in MySQL,
Any suggestions?
Instead of DATE = DATE(NOW()).
Try DATE = NOW()
Try DATE=CURDATE() which will return a date data type. If that doesn't work, try doing some trial and error directly in the database console to see what works and what responses you get. And be sure and update us on the results.
It is very simple.
Use now() function for it.
So it will be :
DATE = now()
Related
I have timestamp column called time in one of my table called tbl_recordings. Its storing data like 2019-04-10 19:51:08. Now I am trying to get result filtered by date but its not working with below query.
$date = 2019-04-10
SELECT * FROM tbl_recordings WHERE time = $date;
here time is timestamp field in my table. Let me know if someone can help me for get out of it. Thanks
Assuming you $date value is a string you should compare the proper date converted values
SELECT * FROM tbl_recordings WHERE date(time) = str_to_date($date, '%Y-%m-%d') ;
Never use date() as it will execute date() on all rows, even those which do not match
Try something like this
... WHERE date >='2019-04-10' AND date <'2019-04-11'
I'm trying to set up a query that will search a MYSQL database and only pull in the rows from the database who's expiry_date is after todays date.
I would also like to be able to work out how many days or weeks there are remaining from todays date to the expiry date of the rows in the database that match the above query.
I think that in order to get the current date I would have to set up a variable of $date = time(); which I will then later be able to use to compare against the expiry_date column in the database. However I am now stumped as what to do to achieve the required result. I'm not exactly a PHP noob but I'm not an expert either, so please go easy on me ;)
Thanks in advance!
If the Column you want to check is a DATE(TIME), try
$sql="SELECT column FROM table WHERE expiry_date > CURDATE()";
If you saved the UNIX timestamp, you can simply use
$sql="SELECT column FROM table WHERE expiry_date > '".time()."'";
If you use the first with "NOW()" or the second, you'll proably get results for the current day.
If this is not acceptable, try "mktime(0, 0, 0)" instead of time();
Use this query
$query = "select timestampdiff(days,'$exipry_date','$now')";
before any thing I would like to thank you here is my problem
I want to write MYSQL query that select the records where insert date equal to modified date from a datetime field and ignore the time.
I tried
UPDATErealty SET modifier = creator WHERE created = updated;
but it worked with the records that was modified in the same time and I want it to ignore the time
date() cuts time leaving only date
UPDATE realty SET `modifier` = `creator` WHERE date(`created`) = date(`updated`);
I have a mysql DATE Field named dDate that is formatted like this: YYY-MM-DD I would like to update all of the records in this field to a different DATE format of MM-DD-YYYY can someone show me how to to this via mySQL query ?
I tried this example but it doesn't work
UPDATE address_email SET dDate = DATE_FORMAT(dDate, '%m-%d-%Y')
and this:
UPDATE address_email SET dDate = DATE(dDate, '%m-%d-%Y')
THANKS SO MUCH FOR THE HELP.
Based on the MySQL docs, it looks like MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format only, and I don't think you can change it.
What you really want to do is format the date when you pull it out of the database, not change the default formatting of the DATE field for your table. When you SELECT, do something like this:
SELECT DATE_FORMAT(dDate, '%m-%d-%Y') FROM address_email;
Have a look at the mysql documentation regarding date_format :
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
UPDATE vechical
SET PDate = DATE_FORMAT('12-08-2012','%d-%m-%y')
WHERE Cust_ID='21';
Please correct query for this
I have events in my MySQL database wich all have a date. When I perform a SQL query to get all the events in the future, I get an error... Although, the date of the events are in the future. When I change my SQL request to select dates in the past, I get those from the future...
The SQL statement below has worked before, but for some reason it stopped working...
I use this SQL request:
$sql = "SELECT * FROM calendar WHERE date >= CURDATE() order by `date`";
I get an empty array as result...
However if I change the query to this, I get all the events in my database:
$sql = "SELECT * FROM calendar WHERE date <= CURDATE() order by `date`";
This is my database data. In my opinion, all data are in the future...
The format of the date table is a default date-type:
When I ask my server about the time echo date("Y-m-d"); I get todays date as result...
So where do I make a mistake?
You may be checking the wrong date field. Do you have a created date as well as a scheduled date?
I could be crazy from the cold medicine I am on at the moment, but your date table can't possibly be the date of your calendar items, the id filed is only an int(2), that seems kind of small.
maybe something simplier? I notice the column name in your table is date, which also is the name of a function date() that returns the date part of a datetime value. If thats the case
$sql = "SELECT * FROM calendar c WHERE c.`date` <= CURDATE() order by `date`";
would do the trick. Even if not mysql itself, the gui app youre using (seems like phpmyadmin to me) might get confused.
(btw, you forgot the closing tick of date in the order by clause)
getting an empty set is meaning nothing is found matching. I would look at your formatting of your date. The only other thing i was thinking is that it is comparing an unmatched type so just returns an empty set.
use DATEDIFF :
DATEDIFF
WHERE DATEDIFF(date, CURDATE) > 0
Before you make your query, run this one:
SET time_zone = '-2:00'; // or whatever your time zone is.
Don't ask me how or why, but I've truncated my table and re-inserted some data and my query seems to work just fine:
$sql = "SELECT * FROM `calendar` WHERE `date` >= CURDATE() order by `date`";
So, despite the fact the problems seems to be solved by truncating my table, I would like to know the answer to the why-question... Anyone can provide me with it?