Time Difference [duplicate] - php

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)

Related

Check if datetime is older than 5 days [duplicate]

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

Convert Postgres interval to PHP DateInterval

I am saving a value into a Postgres10 interval type field.
When I return the field, I get the following: 3 years 10 mons 1 day 02:18:00
The problem I seem to be having, mostly, is that it would also return 1 day 02:18:00 if the duration is not years and months long.
Is there a clean way to convert this value into a PHP DateInterval object?
I figure I can use regex but that seems a little messy to me.
If you change the interval output style to iso_8601 using
SET intervalstyle = 'iso_8601';
You will get output in the correct format for a call to DateInterval::__construct e.g.
SELECT
INTERVAL '6 years 5 months 4 days 3 hours 2 minutes 1 second';
Output:
interval
P6Y5M4DT3H2M1S
Demo on dbfiddle

How to calculate time difference in PHP and store the time difference in mysql database [duplicate]

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 );

Extended hours in php [duplicate]

This question already has answers here:
Calculate time elapsed in php - spans over 24 hours
(2 answers)
Closed 9 years ago.
I have been searching for a while now regarding on how to format hours in php that will show 000:00:00 instead of 00:00:00.
I need this to calculate the duration of records with start and end Date with a datetime data type in mysql.
The problem with my current format ('H:m:s') is it only count 24 hours and when the duration goes higher than that, it will show 01:00:00 instead of 25:00:00.
Try TIMEDIFF() function in MySQL
Example:
SELECT TIMEDIFF('2008-12-31 23:59:59.000001',
'2008-12-30 01:01:01.000002');
-> '46:58:57.999999'

Getting the difference between two MYSQL datetime columns in seconds [duplicate]

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

Categories