Although I have been working with PHP for a while, the one part of it I am still trying to get right is time.
I am creating a simple script that will check if the timestamp is greater than or equal to an hour, and if it is, it will be deleted from the database.
2013-01-03 20:30:25
DELETE FROM tablename WHERE timestamp = ?????
I am not sure how to execute the query to delete values with a timestamp of over an hour from the current time. Any help is greatly appreciated.
DELETE FROM tablename WHERE `timestmap` < DATE_SUB(NOW(), INTERVAL 1 HOUR)
Ref:- date_add and date_sub
First of all, 2013-01-03 20:30:25 is not a timestamp, it is a formatted date. The timestamp for that date would look like this: 1357245025. You can convert it to a timestamp using the strtotime function. You can also work out the timestamp of an hour ago by using strtotime("-1 hour") and performing a comparison on the values.
It might be faster just to do all of this within the MySQL query though, MySQL provides queries to do this, using a query similar to the one that Amit Garg provided.
Related
I am stuck for couple of Days on SQL specific scenario. The scenario is as follows,
I have a table, lets call it traffic which has 2 columns -> date and `vehicle (well many more but those are the two I need to match).
The date column is stored as Unix Timestamp. Now this would have been easy to just compare the current date (obtain from php from time() function) however the trick here is that some of these dates have time attached to them also.
For example if you run strtotime(13-02-2017 13:00) and strtotime(13-02-2017) you will get 2 different results. Basically I only care to match the date and not the time.
So I need some way to select the vehicle and date from the database that are equalled to the current Unix Timestamp but with the trick explained above, so I just need to much the date ONLY if possible.
You can use FROM_UNIXTIME() to convert a timestamp to a datetime, and then use the DATE() function to get the date part of that.
WHERE DATE(FROM_UNIXTIME(date)) = CURDATE()
However, this can't use an index, so another way that can make use of an index is to check if it's in a range of timestamps for the current date:
WHERE date BETWEEN UNIX_TIMESTAMP(CURDATE()) AND UNIX_TIMESTAMP(CURDATE()) + 86399
(there are 86400 seconds in a day).
SELECT * FROM traffic WHERE DATE(date) = DATE(CURRENT_TIMESTAMP);
I want to limit my users to only (be able to) post something every 15 minutes.
So in my SQL query I select NOW() to get the current date and time, and also the user's last post date date_added. I want to compare the two dates and if the difference between now() and date_added is less than 15 minutes, then the user cannot yet post. If it's greater then he can. If less than 15 minutes then I'd like a message like 'Please wait x minutes and y seconds.' So I need some kind of date manipulation/comparison.
How should I approach this. In MySQL or PHP?
You could simply convert the mysql timestamp into a php-date and compare from there
$time = date ("Y-m-d H:i:s", $mysqltime);
You will find lots of useful snippets how to compare dates on the functions documentation: http://php.net/manual/de/function.date.php
Edit: pozs anwser nails it ...
In MySQL: DATE_ADD() or DATE_SUB(), then compare.
In PHP: DateTime->diff().
Something like
SELECT count(*) FROM posts WHERE last_post > DATE_SUB(NOW(), INTERVAL 15 minute);
Is there a way to compare a datetime field in mySQL with the current date and return the time difference? I don't really know where to start so if anyone has an idea, please point me to the right direction.
Here's what I already tried.
SELECT *
FROM `order`
WHERE `dateTimeAdded` - NOW( ) <1;
basically what I am trying to achieve is to get the orders which is saved for more than 8 hours in the database. Thanks!
If you want your query to have a chance of using an index on the datetime column, you should not do manipulation (functions) on the column but only on the other side:
SELECT *
FROM `order`
WHERE dateTimeAdded < NOW( ) - INTERVAL 8 HOUR ;
If u just need the diff of dates use
DATEDIFF(now(), column)
and if you need the time difference use
TIMEDIFF(now(),column)
DATEDIFF(date1,date2);
Taken FROM
i want to add a column named column_ourcountry_time that will show the timestamp of the time when each row is created DATE_ADD INTERVAL 10 HOUR to current time of server in each row.
when i use the TIMESTAMP it saves the current server time,its ok,but i want to store the timestamp of our country,thats +10 hours to the server time.anyone help me get through it.
thanks in advance
yours faithfully
Aiman
Do you mean something like
select addtime(now(), '10:00:00');
because that adds time and keeps days like they are (24 hours) etc.
If the '10:00:00' part is a variable '10' in another column timezone_offset, I think you can use this:
select addtime(now(), concat(timezone_offset, ':00:00')) from table;
-- edit 1
Depending on timezone_offset and the timezone of your MySQL server, you might have to alter timezone_offset before you use addtime:
If MySQL runs UTC +1 and you save timezone_offset compared to UTC, you have to alter all timezone_offset -1, because now() isn't UTC timezone.
-- edit 2
I find it a best practice to store ALL times in UTC because that's the easiest to calculate from and to. If you're doing anything with timezones and offsets: do everything UTC. (Make sure your MySQL server has UTC as timezone as well.)
Try
SELECT NOW() + INTERVAL 10 HOUR
you may use
SET time_zone = '+10:00'
read here
http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html
This will be done with PHP.
I basically want to get the number of rows that were inserted 30 minutes ago.
I have a time field on my table which is type TIMESTAMP and on update it's set to CURRENT_TIMESTAMP.
The date is stored in this format:
2011-05-27 04:29:17
My query is supposed to look something like this, however i just can't do it
SELECT COUNT(*) FROM mytable WHERE UNIX_TIMESTAMP(time) < '.time().'-1800
Where time() is PHP's function that fetches the UNIX time.
What it should basically do is print me the number of rows inserted from now to 30 minutes ago, but i just can't seem to make it work.
Can somebody help?
Small edit:
Another problem i am seeing is that php's function time() displays the unix time which is UTC. The time stored in mysql is probably GMT i.e whatever my computer's time/timezone is set to.
You can easily get rows stored from now to 30 mins ago by simply using:
SELECT count(*) FROM mytable WHERE `time` >= DATE_SUB(UTC_TIMESTAMP, INTERVAL 30 minute)
Usage of UTC_TIMESTAMP is just an example if you're storing your date/time data as UTC_TIMESTAMP(), you can probably use NOW() if necessary, depends on what you're storing really.
**EDIT**
Removed bad pointers and fixed example :)
Do you really need your computer's timezone to be different than UTC? why not just set it to UTC & save yourself the confusion? If that doesn't work, just use dateadd() on mysql to convert your mysql timestamp to UTC when checking?
My suggestion would be to write a small function to convert the mysql timestamp to your PHP timestamp format & load it into mysql. Then all you need to do is to call tmstamp(time_stamp) instead of time_stamp in your query. You can do the reverse too i.e. Convert PHP's "30 minutes ago" timestamp to mysql format and rerun your query (probably easier).
Usually it's just a formatting issue. It's not standardized across programs.