MySQL Interval Expiry from Creation Timestamp - php

I am working on a project where one "lights a virtual candle" and I want to create a cron job that selects all records from the database that are expiring in the next five days, possibly calculated from the databases "created_date" field which type is "Type: TIMESTAMP > CURRENT_TIMESTAMP"
Process:
The candle duration = 30 days
Alert period = 5 days before the 30 days
This is what I have so far (I can do the rest, it is the query I am having problems with)
$query_rsQueryA = "SELECT * FROM $databaseName WHERE created_date + INTERVAL 5 DAY < CURRENT_TIMESTAMP;";
$rsQueryA = mysql_query($query_rsQueryA) or die(mysql_error());
$row_rsQueryA = mysql_fetch_assoc($rsQueryA);
$totalRows_rsQueryA = mysql_num_rows($rsQueryA);
Thanks in advance!

It appears what I have written above actually works, should have tried it first guys, sorry about that!

You can use the following
datediff(now(),date_add(created_date,INTERVAL 25 day)) > 0
So basically its adding 25 day to the created_date and then finding the difference with the current date and if its greater than 0 meaning start sending alert

Related

How do I get a hourly average of a value which is stored per minute

The following table receives a dataset (nearly) every minute via cronjob:
id | timestamp | humanTime | userCount
-----------------------------------------------------
1 1482310202 2016-12-21 09:50:07 120
2 1482310262 2016-12-21 09:51:07 126
3 1482310322 2016-12-21 09:52:06 110
4 1482310381 2016-12-21 09:54:06 131
5 ...
Since the cronjob is a query via network, it is not ensured that there are 60 entries in every hour due to possible timeouts.
I would like to calculate the hourly average of the column userCount for the last X hours.
Does anyone have an idea?
P.S.: If there's no way to do this via sql, i'd like to solve this problem via PHP
You just need to extract the date and hour from time. I'd stick with the human time:
select date(humanTime) as dd, hour(humanTime) as hh, avg(userCount)
from t
group by date(humanTime), hour(humanTime)
order by dd, hh;
If you want the last "n" hours, then include:
where humanTime >= date_sub(now(), interval "n" hour)
(PHP SOLUTION)
Since unix timestamp is in seconds, you can calculate the diff between 2 stamps in hours like this:
$stamp1 = 1482310381;
$stamp2 = 1482310202;
$hours_diff = ($stamp1 - $stamp2) / 60 / 60;
You can iterate the timestamp and the userCount columns simultaneously, and check:
if the timestamps diff is (or greater than) the hour range that I want, I will calculate the sum of userCount I got (add each to a var i.e. $sum) and divide it by the diff between the first timestamp id column and the last timestamp id column.
Hope it helps, Good luck! :)
You can use DATEPART. As here I am providing per hour entry count for one day:
SELECT DATEPART(hh, human_time) AS hour, COUNT(id) id
FROM table
WHERE human_time >= '2016-12-21' AND human_time < '2016-12-21'
GROUP BY DATEPART(hh, human_time)

How to select all records that are 10 minutes to timestamp?

I have timestamps in a table order_onsite_interpreter column called assg_real_timestamp.
I need to get all rows where assg_real_timestamp is 10 minutes before assg_real_timestamp.
The logic is this: I get all rows that are 10 minutes before the session starts(session starts as shown with assg_real_timestamp) and then I send a push notification to the user on a ios app.
So if 10 minutes or less are before the session starts i have to fetch the rows.
This is an example, where I stop. I suspect I have made the wrong query.
SELECT *
WHERE scheduling_type IN ('get_call', 'conference_call')
AND push_notification_sent = 0 AND is_phone = 1
AND assg_real_timestamp <= now() - INTERVAL 10 MINUTE
This is how to get all rows with assg_real_timestamp with a value set in the past 10 minutes.
SELECT * FROM `<table_name>`
WHERE scheduling_type IN ('get_call', 'conference_call')
AND push_notification_sent = 0
AND is_phone = 1
AND assg_real_timestamp BETWEEN DATE_SUB(NOW() , INTERVAL 10 MINUTE) AND NOW()
Mysql: DATE_SUB()
See also: MySQL Select rows where timestamp column between now and 10 minutes ago
So if anyone has to retrieve all rows from the database, by looking at a columns timestamp that's in GMT, where the timestamp is less that 5 minutes from now()
SELECT orderID, scheduling_type, frm_lang, to_lang, customer_id, amount,
onsite_con_phone, assg_frm_date, assg_frm_st, timezone FROM
`order_onsite_interpreter` WHERE scheduling_type IN ('conference_call',
'get_call') AND push_notification_sent =0 AND is_phone =1 AND
DATE_SUB(CONVERT_TZ(DATE_FORMAT(FROM_UNIXTIME(assg_frm_timestamp), '%Y-%c-%d
%T:%f'), '-7:00', '-0:00'),INTERVAL 5 MINUTE) BETWEEN CONVERT_TZ(DATE_SUB(NOW(),
INTERVAL 5 MINUTE), '-7:00', '-0:00') AND CONVERT_TZ(NOW(), '-7:00', '-0:00')
-7:00 is where I need to convert from my server's timezones to GMT timezone. It's a matter of syncing my values to GMT and them i can compare the timestamp and now(). This works, like this:
A session starts at 02:03, meaning that in 02:03 - 00:05 = 01:58 the cron job that's fired every minute should retrieve the rows from order_onsite_interpreter table. After retrieving the row, I then send a push notification to user's app with message 'Your scheduled session is about start in 5 minutes, please login to the app'. And set push_notification_sent = 1 so the user doesn't receive the message anymore in the scope of the five minutes that the query from cron job finds.

Mysql using only a time in date_sub or alternative

I have a mysql field set as "TIME", and I would like to check against that time, using time as well.
I will have a cron run every 10 minutes, where my clients can set reminder messages to be sent out every day at any time of the day or so.
Using the following query does not give me the fields that are from the last 9 minutes:
SELECT `id` FROM `sms_reminders` WHERE (`option` = "weekdays" OR `option_days` LIKE "%4%") AND `time` < DATE_SUB(NOW(), INTERVAL 9 MINUTE)
However, this is giving me mixed results.
My time field is set at 09:50:00 (Time of running the query is 09:53:00).
Thanks.
make few changes in your code use > this instead of <
After altering the query, I flipped the < to > and it is now working.

mySQL - PHP time = time pls 12 hours

I have a database (mySQL) with a time field. I want to write a simple line or two in my php script to add 12 hours to the time IF the time is less than 12:00:00
Please can anyone help (newbie!)
Kind regards.
Philip.
You should use MySQL for that because it is faster and less to write :-)
This doesn't update the column but fetches the time according to your expectation.
Make sure to actually have time column as TIME/DATETIME type.
SELECT
IF(HOUR(timecol) > 12,
DATE_ADD(INTERVAL 12 HOUR, timecol),
timecol
) AS new_timecol
FROM table
Also keep in mind:
1st of month, 23:30 o clock.
+ 12 hours =
2nd of month, 11:30 o clock.
If you want to select the time in 24 hour format, there are flags which simply allow you to do so without adding 12 hours or something.
all you need to do is check if it less than 12 then add an interval of 12 hours or else just put in the time like so :)
SELECT
CASE
WHEN timecol < '12:00:00' THEN ADDTIME(timecol, '12:00:00')
WHEN timecol >= '12:00:00' THEN timecol
END
FROM test
DEMO
EDIT:
with the newly provided information you should incorporate it into the original select like so.
SELECT ff_id, book_cancel, booking_date,
pax_number, room, name, tel_number,
email, voucher_type, voucher_number, notes,
CASE
WHEN time < '12:00:00' THEN ADDTIME(time, '12:00:00')
WHEN time >= '12:00:00' THEN time
END AS time
FROM tables
WHERE booking_date>= '" . date("Y-m-d") . "'
ORDER BY booking_date ASC, time ASC

How to make a reminder in mysql with date stored in unix_timestamp

Please I am making a reminder to start sending mails for events starting in 7 days or less.
That is send mails to attendees to events starting in 0 - 7 days only.
Where I need help is on this line:
...WHERE event_start_date > '$current_time' //currently sends to all attendees for all events starting in future
$current_time = time();
How can I change that to reflect what I need?
Kindly note date is stored in unix_timestamp.
Thank you
You could also do this in MySQL only, without determining the time in PHP first. I don't think it's faster, but it's a lot more readable.:
...WHERE FROM_UNIXTIME(event_start_date) <= DATE_ADD(CURDATE(), INTERVAL 1 WEEK)
However, this also returns events starting before today. If you don't want that, you could use:
...WHERE FROM_UNIXTIME(event_start_date) BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 1 WEEK)
(adapted from the MySQL docs)
Get the current unix timestamp.
$current_time = time();
As it is stored in seconds, you can add to it by adding 60 seconds * 60 minutes * 24 hours * 7 days to get the unix timestamp in a week.
$weekFromNow=$current_time+(60*60*24*7);
Now just find events where the event_start_date is less than or equal to it.
...WHERE event_start_date <= '$weekFromNow'
and event_start_date > '$current_time'
//sends to all attendees for all events starting in future
Edit: as $weekFromNow is one week into the future, the query needs to find all times less than or equal to that value, which is what the query does. If your event_start_date holds some value OTHER than the unix timestamp of the event starting time, let me know.

Categories