PHP / MYSQL Date Calculations for automated billing - mysql

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

Related

Subtract 5 minutes from a date value

I'm currently writing a snippet of code which will look through the database and see if any admins are currently online and show them on the side of the page as being online.
Every time a user browses through a page on my site (when they are logged in) it grabs the current time and updates a column called lastseen.
I have a role column that marks a user from an admin and what I want it to do is query the database for all the admins and check the lastseen column and if that time is within 5 minutes of the current time then output them to a row which I can then take and format later.
This is what I currently tried but nothing came back.
SELECT username, role, lastseen
FROM database.accounts
WHERE lastseen >= DATE_SUB(NOW(), INTERVAL 10 MINUTE) AND role='admin'
I am storing lastseen as date('Y-m-d H-i-s') from PHP so it looks like this
2017-02-18 16:07:42 in the database.
What is your datatype for lastseen, Your query seems correct , just try with
SELECT username, role, lastseen FROM database.accounts
WHERE STR_TO_DATE(lastseen, '%Y-%m-%d %H:%i:%s') >= DATE_SUB(NOW(), INTERVAL 10 MINUTE) AND role='admin'
Also make sure your PHP and MYSQL Server have same timezones

Fetch mysql data of timstamp depending on time given

I have a column with timestamp, contain example value "2014-04-16 18:00:00","2014-04-17 18:00:00"....
Now, if I will call a page before "2014-04-17 12:00:00" I need this value-"2014-04-16 18:00:00"
And if I call my page after "2014-04-17 12:00:00" I need this value "2014-04-17 18:00:00".
I think my question is very complicated to understand, having complications in date & times, please check date & time properly.
I want to fetch this data from DB in mysql, The page I was saying is that where I'm going to add your mysql query.
Thanx in advance
Generalising what your asking for a bit the following will return dates from the previous day if it's before noon and dates from today if it's after noon:
SELECT date_column
FROM yourTable
WHERE DATE(DATE_SUB(NOW(), INTERVAL 12 HOUR)) = DATE(date_column);
Edit:
The WHERE clause First gets the current time (NOW()) and subtracts 12 hours. This wont affect the date unless the time is before 12. This means DATE_SUB(NOW(), INTERVAL 12 HOUR) gives us today if it's after noon and yesterday if it's before.
We then check if the date_column matches the date we've created (using the DATE function so that the time is ignored).
Adding some rows to the SELECT may help you see how these dates are built up.

mysql query to compare 2 types of dates when actual date unknown

Is it posaible to write a query when you don't know any acutal dates? I need to compare transaction dates with funds available dates & update the funds available date by adding a day to the date.
I know MS SQL better than MySQL, but you still should be able to compare them using greater than and less than. If you want to modify the value, you'll want to use the DATE_ADD function (Date and Time functions reference). Comparing transaction date to the funds available date would involve something like WHERE TransTable.TransDate >= CustTable.FundsAvailDate, and then adding one day to the funds available date would be something like SET FundsAvailDate = DATE_ADD(FundsAvailDate, INTERVAL 1 DAY)

MySQL date and hour issue

I have read that MySQL has some issues regarding date type. I want to get a date interval taking into account the hour. My question is can I do a between query, or make a column for the hour as well?
P.S:
select * from main where main.date between 01-06-11 07:59 and 01-06-11 15:59

How do I subtract using SQL in MYSQL between two date time values and retrieve the result in minutes or second?

I want to subtract between two date time values using SQL in MySQL such that I get the interval in minutes or seconds. Any ideas? I want to run a SQL query that retrieves uses from a database who have logged in like 10 minutes from the time.
There are functions TIMEDIFF(expr1,expr2), which returns the value of expr1-expr2, and TIME_TO_SEC(expr3), which expresses expr3 in seconds.
Note that expr1 and expr2 are datetime values, and expr3 is a time value only.
Check this link for more info.
TIMESTAMPDIFF is like TIMEDIFF which Matthew states, except it returns the difference of the two datetimes in whatever units you desire (seconds, minutes, hours, etc).
For example,
SELECT TIMESTAMPDIFF(MINUTE,LogOutTime,LogInTime) AS TimeLoggedIn
FROM LogTable
Would return the number of minutes the user was logged in (assuming you stored this kind of thing in a table like that).
I would do it like this - fetch where last activity is within 10 mins of now
SELECT * FROM table WHERE last_activity >= DATE_SUB(NOW(), INTERVAL 10 MINUTE)
SELECT TIMESTAMPDIFF(MINUTE,LogOutTime,LogInTime) AS TimeLoggedIn
FROM LogTable
This example shall ruin the time if its used by using millitary time. So for calculating millitairy time I do not recommend it Because it outputs negative values.
You can try and cast them to Unix Time stamp, and take the difference.