I am trying to get the records from php that every row have following table:
[created_at] => 2022-10-15 08:17:52
I want get record that are created last minute.
for that i my php file:
SELECT * FROM customer_billing_details WHERE DATE_ADD(created_at, INTERVAL 1 MINUTE) >= NOW();
but it returning the same row even after it is more than 1 minute old,
I dont able to understand what is happening.
You do not need >= NOW(). the interval will automatically handle the time interval. use something like
SELECT *
FROM `customer_billing_details`
WHERE `created_at` >= CURRENT_TIMESTAMP - INTERVAL 5 MINUTE;
Change 5 minutes to 1 or 2 according to your needs.
SELECT *
FROM SESSIONS
WHERE TIME_TO_SEC(TIMEDIFF(NOW(), SESSION_CREATED)) / 3600 >= 24
This give me 2 results
DELETE FROM SESSIONS
WHERE TIME_TO_SEC(TIMEDIFF(NOW(), SESSION_CREATED)) / 3600 >= 24
And this give me: "Error Code: 1292. Truncated incorrect time value"
SESSION_CREATED is TIMESTAMP Datatype
Actual data:
SESSION_ID SESSION_CREATED
223133 2017-05-22 07:14:34
223134 2017-05-22 07:14:36
How can the select work but not the delete?
Why are you using such a complicated expression? Why not just do:
DELETE FROM SESSIONS
WHERE SESSION_CREATED < NOW() - INTERVAL 1 DAY;
As for why your code might fail, it is using timediff() which is limited to the range of the time data type. And this is:
MySQL retrieves and displays TIME values in 'HH:MM:SS' format (or
'HHH:MM:SS' format for large hours values). TIME values may range from
'-838:59:59' to '838:59:59'.
Because you are using NOW(), the values change from one iteration to the next. You just happened to run the SELECT when the data wasn't too old and then the DELETE when it was.
Example for Timediff using TIMESTAMPDIFF on MySQL:
To use TIMESTAMPDIFF, define the unit (SECOND, MINUTE, HOUR...), the initial and end date (must be timestamp's datatype).
ROUND( TIMESTAMPDIFF(HOUR, initial_date, CURRENT_TIMESTAMP()) / 24, 2 )
I have got a table with 2 columns epoch_start and epoch_end.
I want to find the difference in days of these 2 epochs.
The problem i am facing is that the above columns are character varying(5000) type.
The query im running is
select datediff(day,'1459762341','1450762341') as numdays;
The error i get is
ERROR: invalid input syntax for type timestamp: "1459762341"
I have found the solution -
To get timestamp from epoch -
SELECT (TIMESTAMP 'epoch' + '1459762341' * INTERVAL '1 Second ') as
mytimestamp
For datediff between two epochs -
select datediff(day,(TIMESTAMP 'epoch' + '1458762341' * INTERVAL '1 Second '), (TIMESTAMP 'epoch' + '1459762341' * INTERVAL '1 Second ')) as numdays;
"epoc" time is just the number of seconds from 1/1/1970, so its just some counter of the number of seconds from that date.
So, epic_start can really be thought of as start_seconds, and epic_end is really just end_seconds.
The fact that they are the count of the number of seconds from the same starting point is the only thing that really matters.
To get the number of days between two numbers representing seconds from the same starting point:
days = (end_seconds - start_seconds)/60/60/24
or
SELECT (end_seconds - epic_start)/60/60/24 AS numdays
Redshift will return an integer value without the decimal portion, so if the formula returns 1.9, numdays will be 1.
You are passing wrong parameters in datediff().
SELECT DATEDIFF('2014-11-30','2014-11-29') AS DiffDate
Above will return the difference in days between two given dates.
Read more on datediff() here datediff
I have a date time field in a MySQL database and wish to output the result to the nearest hour.
e.g. 2012-04-01 00:00:01 should read 2012-04-01 00:00:00
Update: I think https://stackoverflow.com/a/21330407/480943 is a better answer.
You can do it with some date arithmetic:
SELECT some_columns,
DATE_ADD(
DATE_FORMAT(the_date, "%Y-%m-%d %H:00:00"),
INTERVAL IF(MINUTE(the_date) < 30, 0, 1) HOUR
) AS the_rounded_date
FROM your_table
Explanations:
DATE_FORMAT: DATE_FORMAT(the_date, "%Y-%m-%d %H:00:00") returns the date truncated down to the nearest hour (sets the minute and second parts to zero).
MINUTE: MINUTE(the_date) gets the minute value of the date.
IF: This is a conditional; if the value in parameter 1 is true, then it returns parameter 2, otherwise it returns parameter 3. So IF(MINUTE(the_date) < 30, 0, 1) means "If the minute value is less than 30, return 0, otherwise return 1". This is what we're going to use to round -- it's the number of hours to add back on.
DATE_ADD: This adds the number of hours for the round into the result.
Half of the hour is a 30 minutes. Simply add 30 minutes to timestamp and truncate minutes and seconds.
SELECT DATE_FORMAT(DATE_ADD(timestamp_column, INTERVAL 30 MINUTE),'%Y-%m-%d %H:00:00') FROM table
soul's first solution truncates instead of rounding and the second solution doesn't work with Daylight Savings cases such as:
select FROM_UNIXTIME(UNIX_TIMESTAMP('2012-03-11 2:14:00') - MOD(UNIX_TIMESTAMP('2012-03-11 2:14:00'),300));
Here is an alternate method (1):
DATE_ADD(
tick,
INTERVAL (IF((MINUTE(tick)*60)+SECOND(tick) < 1800, 0, 3600) - (MINUTE(tick)*60)+SECOND(tick)) SECOND
)
If you don't need to worry about seconds you can simplify it like this (2):
DATE_ADD(
tick,
INTERVAL (IF(MINUTE(tick) < 30, 0, 60) - MINUTE(tick)) MINUTE
)
Or if you prefer to truncate instead of round, here is simpler version of soul's method (3):
DATE_SUB(tick, INTERVAL MINUTE(tick)*60+SECOND(tick) SECOND)
EDIT: I profiled some of these queries on my local machine and found that for 100,000 rows the average times were as follows:
soul's UNIXTIME method: 0.0423 ms (fast, but doesn't work with DST)
My method 3: 0.1255 ms
My method 2: 0.1289 ms
Ben Lee's DATE_FORMAT method: 0.1495 ms
My method 1: 0.1506 ms
From How to round a DateTime in MySQL?:
It's a little nasty when you do it with datetime data types; a nice candidate for a stored function.
DATE_SUB(DATE_SUB(time, INTERVAL MOD(MINUTE(time),5) MINUTE ),
INTERVAL SECOND(time) SECOND)
It's easier when you use UNIXTIME timestamps but that's limited to a 1970 - 2038 date range.
FROM_UNIXTIME(UNIX_TIMESTAMP(time) - MOD(UNIX_TIMESTAMP(time),300))
Good luck.
To round down to the current hour, select:
FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP(column_name) / 3600) * 3600).
The value is expressed in the current time zone doc
This will return the next hour, that is '2012-01-02 18:02:30' will be converted into '2012-01-02 19:00:00'
TIMESTAMPADD(HOUR,
TIMESTAMPDIFF(HOUR,CURDATE(),timestamp_column_name),
CURDATE())
Instead of CURDATE() you can use an arbitrary date, for example '2000-01-01'
Not sure if there could be problems using CURDATE() if the system date changes between the two calls to the function, don't know if Mysql would call both at the same time.
to get the nearest hour would be:
TIMESTAMPADD(MINUTE,
ROUND(TIMESTAMPDIFF(MINUTE,CURDATE(),timestamp_column_name)/60)*60,
CURDATE())
changing 60 by 15 would get the nearest 15 minutes interval, using SECOND you can get the nearest desired second interval, etc.
To get the previous hour use TRUNCATE() or FLOOR() instead of ROUND().
Hope this helps.
If you need to round just time to next hour you may use this:
SELECT TIME_FORMAT(
ADDTIME(
TIMEDIFF('16:15', '10:00'), '00:59:00'
),
'%H:00:00'
)
I think this is the best way, since it also will use the least amount of resources-
date_add(date(date_completed), interval hour(date_completed) hour) as date_hr
I'm trying to offset a timezone error from PHP. All times recorded in table 'test' was ahead by two hours. What I want is to update each record by minusing two hours from the time that is already there.
I tried:
UPDATE test
SET LastModifiedDate = SUBTIME( LastModifiedDate, '02:00:00' )
But this just updates all fields with the same value.
Please assist
tthanks
update test set LastModifiedDate = LastModifiedDate - interval 2 hour;
Use the DATE_SUB() function:
UPDATE test SET LastModifiedDate = DATE_SUB(LastModifiedDate, INTERVAL 2 HOUR)
Test it first to be certain it's doing what you want:
SELECT LastModifiedDate, DATE_SUB(LastModifiedDate, INTERVAL 2 HOUR) FROM test;
update test set LastModifiedDate = adddate(LastModifiedDate, interval -2 hour);
this will modify all your dates to -2 hour. you can narrow down the result in "where" section of the query by targeting specific rows.