This question already has answers here:
MySQL selecting yesterday's date
(8 answers)
Closed 9 years ago.
I want to set dealend to be Yesterday instead of now. Is there a way to do it in this command line?
$query = mysqli_query($myConnection, "UPDATE bookdeals SET dealend='now()' WHERE id='$pid'") or die (mysqli_error($myConnection));
... SET dealend = now() - INTERVAL 1 DAY
Note that with the 'now()' you're using, you're not using a function called "now". You're trying to set your table field to be a string whose contents are the letters n, o, w etc.... Quotes turn things into strings and those things lose their specialty once they've been stringed.
now() is a SQL function that is determined by the starting time of the transaction. You can changed the value of it by calling
SET TIMESTAMP = ...
However, for this to work it would need to be in the same transaction as your UPDATE query. What I would recommend instead is doing something like:
UPDATE bookdeals SET dealend=DATE_SUB(NOW(), INTERVAL 1 DAYS) WHERE id='$pid'
This uses the MySQL SUB_DATE() function, which will subtract one day from the current time.
You could use the MySQL DATE_SUB function
... dealend=DATE_SUB(now(),INTERVAL 1 DAY) ...
Read here for more information about MySQL date function
Related
This question already has answers here:
Datetime equal or greater than today in MySQL
(10 answers)
MySQL Where DateTime is greater than today
(3 answers)
Selecting entries by date - >= NOW(), MySQL
(6 answers)
Select record(s) from mysql table where date is greater than or equal to today
(3 answers)
MySQL query select all were date is equal to today on datetime
(2 answers)
Closed 3 years ago.
Would somebody mind helping me with this?
I'm trying to select all bookings from my table with a start date of today or in the future. The issue i'm having is that I already have a limit (pagination) and sort.
$today = date("Y-m-d");
$sql = "
SELECT *
FROM `bookings`
WHERE startdate >= $today
ORDER BY `startdate` ASC
LIMIT $offset, $no_of_records_per_page";
Edit: The code above displays all bookings regardless of date, rather than just present/future bookings.
Look at the generated SQL statement -- the current date in the SQL query should be something like '2020-01-02' (including the single quotes). Without the quotes, you'd get weird behavior like the database doing the subtraction and comparing the date against 2020-01-02 = 2017!.
WHERE startdate >= $today
Your immediate problem is that you are not surrounding the date variable with single quotes, so you end up with something like where start_date >= 2020-01-13. MySQL sees an arithmetic operation (2020 minus 1 minus 13 = 2006) and happily executes it. Now it needs to compare column startdate (which is of date datatype or the like) to an integer: for this, it implicitely casts startdate to unsigned: this generates a number like 20200113, which is much bigger than 2006. This is not what you intend.
This woud not happen if you were using parameterized queries.
But bottom line, why bother computing the current date from PHP when MySQL has a built-in for that? Just do: WHERE startdate >= CURRENT_DATE and you are all set.
This question already has answers here:
MYSQL Select rows where date is older than datetime
(3 answers)
Closed 6 years ago.
Hey I have a table that every row contains several data and one of them is the date , I am trying to write a code in php that will run on the table and delete every row that its date have been expired for example if the date today is 06/01/2017 than every item that its date is smaller than today should be deleted, the thing is I don't really have an idea on how to write this function so if someone can send me a tutorial or example of how should I do it that will be great.
DELETE FROM your_table WHERE your_date_column < NOW() for what concerns the SQL query... Then simply use PDO to execute it
Your question actually is quite general, sounds like homework... Otherwise, posting some code attempts is appreciated as much as showing you did try / make some research beforehand. :)
You'll need to create a page that sets tomorrow's date and executes the mysql command:
$conn = new mysqli($db_servername, $db_username, $db_password, $db_dbname);
if ($conn->connect_error) { }
$tomorrow = new DateTime('tomorrow');
$tomorrow->format('Y-m-d');
$sql = "DELETE FROM 'yourtable' WHERE Date < '{$tomorrow}'";
$result = $conn->query($sql);
you can use this query for delete past records
mysql> delete from your_table where date < curdate();
This question already has answers here:
MySQL convert date string to Unix timestamp
(4 answers)
Closed 8 years ago.
there is a table with these fields
month
year
day
and these files are contain numbers like
year = 2001 and month = 10 and day = 25
and i have time stamp in php
so i need to compare these in mysql
something like:
select * from times where mktime(year,month,day)<1235478554
is there any function in mysql for this?
i need to make time stamp in mysql with the mysql fields...
UPDATE 1 :
i used like this and not worked
SELECT * from work_result where UNIX_TIMESTAMP('year-month-day 00:00:00')<1
UPDATE2:
There's UNIX_TIMESTAMP which will get you the Unix timestamp of a given date string. Which means you need to build a date string inline in your MySQL query. Doing so is considered bad style, you should propably be storing a Unix timestamp within your database.
This question already has answers here:
Add 2 hours to current time in MySQL?
(5 answers)
Closed 9 years ago.
I have a table in database for meeting schedules. It has 2 columns named start and end and since I dont have access to the php script which fills this table with new data, I am not sure in which format it is.
But PHPMyAdmin shows taht the columns for start and end are varchar(15) So I guess it should be datetime compatible.
Example in DB: 1378033200
Which shows as 01 September 2013
My question is, I want to pull the meetings and show them in a html page, but I do not want meetings which are older than 2 days ago (server time) to show up. What will be the query?
SELECT * FROM schedules ORDER BY start
Something like
SELECT * FROM schedules ORDER BY start WHERE start > 2 days ago
I tried this but it seems it does nothing!
SELECT *
FROM schedules
WHERE COALESCE(start, 0) < CURDATE() - INTERVAL 2 DAY
ORDER BY start
But PHPMyAdmin shows taht the columns for start and end are
varchar(15) So I guess it should be datetime compatible.
You've guessed wrong. Strings are only sortable as strings. Which means, unless you're using a sortable date formats (YYYY/MM/DD being one: I'm not aware of others) you'll have to parse all the results and do the calculation by yourself in PHP (otherwise, 13/11/2000 will come before 14/01/2000). Alternatively, you might wanna use the proper type for your column: datetime, date or timestamp. Once you'll do that, you'll be able to query your db and compare dates with < and > operators.
For the 2 days ago part, you'd like to know that MySql has a built in NOW variable to which you can sum/subtract days. If you'll design your db correctly, you won't even have to touch PHP (which a desiderable thing).
Try this:
SELECT *
FROM schedules
WHERE COALESCE(start, 0) < DATE_SUB(NOW(), INTERVAL 2 DAYS)
ORDER BY start
This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 7 years ago.
Basically I have a simple login form. In the database I have a 'last_logged' column and would like to update it with the current date and time every time someone logs in.
I currently have this query:
UPDATE users SET last_logged = "NOW()" WHERE id = 1
But it doesn't update the column to the current date. Any ideas why?
Remove the quotes from NOW(). As a function call, it should be unquoted.
UPDATE users SET last_logged = NOW() WHERE id = 1
MS SQL uses GETDATE() rather than NOW()
(Just an FYI)
In SQL-Server I now use SYSDATETIME():
DECLARE #now DATETIME = DATEADD(dd,DATEDIFF(dd,'19000101',SYSDATETIME()),'19000101');