I'm using a Laravel application that inserts timestamps based upon 'timezone' => 'America/New_York'. All of my data inserted is the correct date time. Which should be expected. I know that the MySQL's own timezone setting doesn't effect the inserted data.
However, I want to retrieve records from 30 mins ago. But when I use MySQL NOW() function its the wrong time. So I set the time_zone to America/New_York that gives me the correct NOW() but all the record dates have been mutated.
SELECT id, updated_at FROM my_table Where id = 6;
Gives me the correct date in my update_at field;
So to select my records within the past 30 mins is use:
SET #PAST_TIME = DATE_SUB(NOW(), INTERVAL 30 MINUTE);
SELECT id, updated_at, #PAST_TIME as past_time FROM my_table Where updated_at >= #PAST_TIME;
Which returns practically every record in my set and as you can see, the past_time is incorrect (I ran this at 9:31 ET).
Recognizing that the time mysql is out of sync, I set the timezone to
SET time_zone = 'America/New_York';
SET #PAST_TIME = DATE_SUB(NOW(), INTERVAL 30 MINUTE);
SELECT id, updated_at, #PAST_TIME as past_time FROM my_table Where updated_at >= #PAST_TIME;
Which outputs the correct PAST_TIME but all the update_at records are mutated. But it shouldn't return some of those records anyway because the mutated results are greater than PAST_TIME
How do I stop the column mutation? while still being able to select my records?
You can think of a timestamp column as essentially storing a UTC date and time. It really does not matter what the session time zone setting was when the timestamp value was stored if you are initializing it with a function such as now() or current_timestamp(); the date and time will be interpreted in the current time zone and converted to UTC (of course, now() returns a value that is time zone dependent but regardless of what the current session time zone is you should end up with the same UTC date and time after conversion). However, how the timestamp is displayed very much depends on what the current session time zone is, for the timestamp will be converted back from UTC to whatever the current time zone in effect is.
(On the other hand, if you have a datetime column initialized with now() what will be stored will very much depend on the current time zone because there is never any time zone conversion done. But then it will always be displayed the same regardless of what the current session time zone is in effect.)
When you want to retrieve records that were updated within the last 30 minutes, it's natural to have in your query somewhere DATE_SUB(NOW(), INTERVAL 30 MINUTE). When you are comparing NOW() or a value computed from NOW() with a timestamp column, again it should not make any difference what the current time zone is. The timestamp column has implicit time zone information (UTC) and you should retrieve the same records regardless. As I would expect, I see exactly the same id values being retrieved before and after you set the America/New_York time zone (one might see some difference due to the time lapse between the two queries; fewer rows might now have been updated in the last 30 minutes). The difference, which is to be expected, is how the dates and times are displayed.
You do want to set the America/New_York time zone just so the dates and times jive with your local time. But you should still be getting the same records regardless.
Related
I am querying a table that has a datetime column and the value is in the format time stamp with time zone. I've tried doing a select hour(timestamp,-5) as NTime and different variants of that, but the furthest I get is an error stating the timestamp is not a valid name/type. I'm officially going off the deep end on this....
Basically, I just need the new alias column values to be a time stamp that is 5 hours behind the timestamp that is in the original table. Thank you all in advance!!
MariaDB / MySQL doesn't have a "timestamp with timezone" data type.
DATETIMEs are simple wall time, and TIMESTAMPs are UNIX time_t timestamps (the number of seconds since 1970-01-01T00:00:00UTC).
You can convert DATETIME values from one time zone to another by with tz_convert().
SELECT tz_convert('2022-04-08 21:53', 'America/Chicago', 'UTC')
for example.
Or, just to do date arithmetic you can do stuff like this.
SELECT '2022-04-08 21:53' - INTERVAL 5 HOUR
I want to UPDATE my data in scheduled time. My problem is that I cant equal the date that I enter in my database in the current real time date. For example, I have 2015/24/9 19:50:00 in my database, now I want to equal it to the current real time date so that I can update a specific row in the database. If I don't do that, the amount field will just multiply 5 in every row. I want to multiply the amount by 5 in a specific row and time
Code:
CREATE EVENT myeventsdasa11s
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 MINUTE
ON COMPLETION PRESERVE
DO
UPDATE messagesd
SET amount = amount*5
WHERE DATE = (the the current real time date);
DATETIME and TIMESTAMP values are, like floating-point values, difficult to compare for numerical equality. In other words, if it happens that NOW() = datetimestamp, it's a lucky accident. This is especially true when processing events: the event actually starts to run shortly after the scheduled time.
So, instead of saying something like this
`DATE` = NOW()
say something like this
`DATE` BETWEEN NOW() - INTERVAL 10 SECOND
AND NOW() + INTERVAL 10 SECOND
Of course, such a narrow time interval makes you critically dependent on the time accuracy of the event scheduler. You'd be better off adding a LAST_UPDATED column of DATETIME type to your table, then doing this update.
UPDATE messagesd
SET amount = amount * 5,
LAST_UPDATED = `DATE`
WHERE `DATE` >= NOW() - INTERVAL 1 MINUTE
AND (`DATE` > LAST_UPDATED OR LAST_UPDATED IS NULL)
That way, every time your event runs you'll update all the rows that are due for update, but haven't yet been updated. This is not dependent on the precise time an event runs. The - INTERVAL 1 MINUTE allows the event to be up to a minute late running and still function correctly.
If you need to schedule another update for the future for a particular row, change the value of the DATE column but don't touch the LAST_UPDATED column.
Are you just looking for CURDATE()?
CREATE EVENT myeventsdasa11s
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 MINUTE
ON COMPLETION PRESERVE
DO
UPDATE messagesd
SET amount = amount * 5
WHERE DATE = CURDATE();
If you need the current real date time use mysql NOW()
CREATE EVENT myeventsdasa11s
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 MINUTE
ON COMPLETION PRESERVE
DO
UPDATE messagesd
SET amount = amount*5
WHERE DATE = NOW();
I'm having my application checking the database for expired members.
I'm using:UPDATE members SET enabled=0 WHERE now() >= time
That runs every 30 seconds. It was first running at 10 seconds but I didn't know if that was an issue so I made it 30 seconds instead. That's the only thing I have running and my timestamp goes from the time I set and sets it to the time the SQL command is executed. Is there something I'm doing wrong?
Isn't this the expected result? You check the timestamp compared to now() (now() is always greater than time ) and you always update the field. The timestamp is updated also that' s why you get the execution time.
I have a value stored as a DateTime (Paris date-time).
How can I, into a VIEW, know if a stored date is before or equal to NOW(), with NOW() at Paris TimeZone in any case?
PS : I do not have any control over the SQL server.
To Ensure that the date is in paris time zone you can use CONVERT_TZ to convert between time zones. For example the following query will compare the NOW() date with the stored date and gives you the difference (in days) between them, ensuring that the two dates are in a specific time zone, (I don't know the time zone of paris but this is just an example):
select datediff(
Convert_TZ(Now(),"SYSTEM","-08:00"),
Convert_Tz(AddedIn,"SYSTEM","-08:00")
)
from TableName
System returns your current time zone, and for -08:00 is the time zone that you want to convert to as an offset, you can use the name of the time zone or it's offset as specified MySQL Time zone design pattern.
Hope this will help;
I need to pull a variable amount of days of data from a MySQL database that stores the date as a UNIX timestamp in an int column.
For example, if I need the last 5 days of data, what would my query be?
(All queries would start from current date and would go back x amount of days).
Timestamp is considered one of the Date and Time types and therefore any of the Date Time Functions can be used on it.
SELECT * FROM your_table
WHERE Ftimestamp_column > UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 5 DAY));
I've never tried it but there's a MySQL function to convert unix timestamps into MySQL dates and then you can use DATE_SUB() or whatever. http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_from-unixtime