This question already has answers here:
Select rows from MySQL table where PHP timestamp is older than X
(4 answers)
Closed 8 years ago.
I have a cron job that is executing my script every minute, and in that script I need to get rows that are in range from exactly 5 minutes to 5 minutes and 59 seconds. I am keeping the creation time as UNIX timestamp.
You can try this Query in your CronJob
SELECT *
FROM my_table
WHERE
my_timestamp > DATE_SUB(now(), INTERVAL 5 MINUTE)
Related
This question already has answers here:
SELECT all records that are 30 days old
(4 answers)
Closed 1 year ago.
So I am going to make a cron job in cPanel that runs every day, but not sure how the query should look like. I have a datetime column with this type of value: 2021-04-06 14:12:06. How can select from table where datetime column is older than 5 days?
You can use date arithmetic. Assuming you mean "day" without the current time value:
where datetime_col < curdate() - interval 5 day
If you want the reference to be the current time rather than the current date:
where datetime_col < now() - interval 5 day
This question already has answers here:
MySQL Query - Records between Today and Last 30 Days
(7 answers)
SELECT all records that are 30 days old
(4 answers)
Getting mysql result from the last 30 days [duplicate]
(4 answers)
Get from database but only last 30 days
(3 answers)
Closed 2 years ago.
I have a table with a column name "ordered_at" where I have entered the value of time(). How can I select records from the last 30 days?
You could use datediff to get the number of days passed between current_date and the value in the column:
SELECT *
FROM mytable
WHERE DATEDIFF(CURRENT_DATE, ordered_at) <= 30
This question already has answers here:
adding 30 minutes to datetime php/mysql
(6 answers)
PHP Adding 15 minutes to Time value
(9 answers)
Closed 2 years ago.
So basically I need to add 30mins to the current time and send it as a time stamp in database.
date_default_timezone_set('Asia/Hong_Kong');
$today = date("H:i:s");
I.E i need current time : 10:00 sent time to database 10:30.
I know the codes in sending it to the database i just want to know how to add certain mins or seconds to the current time.
You would use the database time for this:
now() + interval 30 minute
You could set a column with the value as:
update t
set col = now() + interval 30 minute
where id = 1;
Pretty simple, but go with the database solution if possible:
$today = date("H:i:s", strtotime(“+30 minutes”);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
MYSQL - datetime to seconds
If I have a datetime column in a mysql table, how can I get the difference between NOW() and that column in seconds?
by TIMEDIFF you can get the differense by seconds
SELECT TIME_TO_SEC(TIMEDIFF('2007-01-09 10:24:46','2007-01-09 10:23:46'));
or this
SELECT DATEDIFF('2007-12-31 10:02:00','2007-12-30 12:01:01') * 24*60*60;
// result: 86400 the difference in seconds for days.
or:
SELECT TIMESTAMPDIFF(SECOND,'2007-12-30 12:01:01','2007-12-31 10:02:00');
// result: 79259 the difference in seconds with the time.
Try this: SELECT NOW() - my_col AS my_diff
This question already has answers here:
Timestamp Difference In Hours for PostgreSQL
(9 answers)
Closed 8 years ago.
I am new to PostgresQL and PHP and am working with a PostgresQL Timestamp object and am trying to find the difference between now and that timestamp to display in terms of years, months, and days. Is there to do this?
Thanks!
You can just subtract two timestamps, the result is an interval.
To get "now" you can use now() or current_timestamp (among others).
SELECT now() - '2010-02-21 20:11:32';
This will display something like this, though:
830 days 23:00:50.127241
To get a justified representation, use age() or justify_interval()
SELECT justify_interval(now() - '2010-02-21 20:11:32');
Displays the same value in a format like you seem to be after:
2 years 3 mons 20 days 23:01:34.095813
If you want a particular output format use to_char()
select age(now(), '2010-01-02 12:34:35');
The complete version:
select substring(a from 1 for (position('days' in a) + 3))
from (select (age(now(), '2010-01-02 12:34:35'))::text) s(a)
;
substring
------------------------
2 years 4 mons 30 days
(1 row)