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
Related
This question already has answers here:
Is there a way to subtract an amount of days from a date in SQL?
(4 answers)
Closed 7 months ago.
I want to subtract days from the db column and then compare it with the current date using DATE_ADD function. Is this possible?
$valid_status_query->whereRaw('DATE_ADD("d",-3, "date_to")', '<=', Carbon::now()->format('Y-m-d'));
For example, if the column date is 2022-07-20 then it should use 2022-07-17 to compare in where clause at the end.
Use it like this:
$valid_status_query->whereRaw("DATE_SUB(date_to, INTERVAL 3 DAY) <= ?", [Carbon::now()->toDateString()]);
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:
How to get time difference in minutes in PHP
(21 answers)
Closed 7 years ago.
I have a table in the database with the name attendance. It has three columns: intime, outtime and work_hours.
I have date and time in intime and outime. Now I want to write the php code to calculate the time difference in the format hh:mm:ss to store into the column work_hour of attendance table.
Please help me out.
timediff returns the difference between two datetimes as a time value:
UPDATE attendance
SET work_hours = TIMEDIFF (outtime, intime)
I have one simple solution, but you will find better. Just try this.
For this you need to convert both of your outtime and intime in seconds using
$working_time_in_seconds = strtotime($outtime) - strtotime($intime);
echo date('H:i:s', $working_time_in_seconds );
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)
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)