Get all the sessions 15 mins prior to start time - mysql

I'm trying to get all the session list who are going to start in 15 minutes. The problem is with client and server timezones and conversions. I've this code so far.
MySQL server is on a different machine and client (nodejs server) is running on another machine.
sql = 'SELECT * FROM sessions
WHERE FROM_UNIXTIME(?) BETWEEN SUBTIME(session.start, "0:15:0") AND session.start'
values = [Math.round(Date.now()/1000)] // convert milliseconds to seconds

I'm expecting something like this:
select s.*
from sessions s
where s.start >= now() and s.start <= date_add(now(), interval 15 minute);
One reason your query doesn't work is because between expects the first comparison value to be less than the second.

Related

MySQL DATE_ADD() not working

In my database I have a table containing some business offers. One of the columns is expire, which contains the date a certain offer expires.
I want to select all offers which expire in 10 days. Here is my code:
SELECT * FROM offers WHERE TIME_ADD(NOW(), INTERVAL 10 DAYS) = expire;
I want to select all columns from offers, where the expiry date is equal date now plus 10 days (so they expire in 10 days). MySQL doesn't let me do that, it marks "=expire" as an error
Syntax error unexpected 'expire' (expire).
Why is that? ( I'm working on MySQL workbench btw)
If you want to find all offers that expire in 10 days, but you're not concerned as to what time in 10 days that they expire, then you can use the following:
SELECT *
FROM offers
WHERE DATE(expire) = DATE_ADD(CURRENT_DATE(), INTERVAL 10 DAYS);
If however you want to find all offers that expire in 10 days, to the exact second, then you can use the following instead:
SELECT *
FROM offers
WHERE expire = DATE_ADD(NOW(), INTERVAL 10 DAYS);
Notice how the first query uses CURRENT_DATE(), which will return only a date value - 2018-08-03. Whereas the second query uses NOW(), which will return a datetime value - 2018-08-03 08:29:00.
replace INTERVAL 10 DAYS with INTERVAL 10 DAY

SQL Updating TImeStamp To 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.

Working with a non-standard date time format in MySQL

I have a system that auto-populates a mysql database with data. Unfortunately the the only time value is being stored in the format YYYYMMDDHHMMSS.SSS. Have a script that runs against the data in the database in a 30 minute cron to alert on certain data trends. The problem that I'm having is that the data in the database goes back 7 days. I only want the script to run against the past 30 minutes of data.
I tried using something like:
SELECT foo FROM table WHERE StartTime >= DATE_SUB(NOW(), INTERVAL 30 MINUTES);
Unfortunately this didn't work, I'm assuming because the stored time formation does not match a standard SQL Timestamp format.
Can anyone suggest an alternate method? Thanks.
StartTime >= DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 30 MINUTE),'%Y%m%d%H%i%s.000')

PHP / MYSQL Date Calculations for automated billing

I am sending 'reminder' messages every 30 days, and was wondering how to calculate the time based on the timestamp I placed on the originating users signup date.
I will have a cron job run every day to query and send the messages, but I am unsure how to query the date so it will select the appropriate accounts each day and send the messages.
Any sure fire ways to do this?
Use the DATEDIFF function to return the difference between then and NOW() in days.
Something like the query below should fit. If you can give me any more details on what / how you wish to query I can make this SQL more specific. This current query selects users whose signup_date was 30 or more days ago:
SELECT * FROM users WHERE DATE_ADD(signup_date, INTERVAL 30 DAY) <= NOW()
For further information:
MySQL Date and Time Functions
Some notes on INTERVAL usage

Mysql database not showing the current date for NOW() function

I am using the NOW() with insert query so that I can get the currentTime when the query is triggered. But I get a time of 12 hours back in the database. How can I get the current Time ? Please help.
Perform this query right after you've been connected to the database:
SET time_zone = '+05:30';
Where it is your valid timezone offset in the quotes.
This might happen if the Timezone in your server configuration is different. Assuming that you are on a shared host, try adding the following lines when you are querying:
mysql_query("SET time_zone = '+05:30';");
Source: http://wiki.dreamhost.com/Running_web_scripts_in_your_Timezone
Else try adding the following to your .htaccess file
SetEnv TZ Asia/Colombo
A full list of all the Timezones http://www.php.net/manual/en/timezones.php
You can do something like this
SELECT * FROM tbl WHERE datetime < NOW() - INTERVAL 12 HOURS
I believe you can do, in your SQL query:
DATE_ADD(now(), INTERVAL 12 HOUR);
This will take the current time and subtract add 12 hours from to it.