How to send email reminders on different conditions? - php

I have a table called reservations which has columns start_date and end_date.
If date_diff(start_date and end_date) is 30 then I need to send an email on 8,2 and 1 days before end_date. same with the below..
60 – 15,4 & 1
120 and above – 30, 8, 2 & 1...
any idea how to do this ??

set up cronjob to do this kind of stuff.

write 3 separate sql statements for your query get these to work.
then put these into a mysql_query in php setting a variable if row count >0
using isset on the variable into email smtp script
set up a cron job to check every morning.

SELECT *,DATEDIFF(CURDATE(), end_date) as DIFF FROM TABLE WHERE DATEDIFF(CURDATE(), end_date) in (1,2,8)

What you can do is you create 2 tables, 1 for the main reservation information and the 2nd one will be for the storing of the send dates (or send reminder dates).
When the saving the record you will then retrieve the difference between the current date and the end_date; if difference will be 30 then you'll have to store send_dates 8,2 and 1 days before the end_date; same with the other conditions you have.
Then create a cron job (that will run daily) to check the 2nd table of the send_dates = current date. Then email if conditions is met.
Hope this is the one you needed

Related

How to compare dates from php database with the today's date

I have created a mysql table as shown below.
I would like to compare the dates in the table with the current date by using if else statement, and send notifications to the user one day before, one day after and one the same date
For instance, let's say today's date is 18/10, and expiry date for Milk is 20/10, so I want to send notifications on 19th, 20th, and 21st.
So, if you can help me with comparison code and the code for sending notification (such as email) that should be under if statement I would really appreciate it :).
My question might seem easy but I am new to php I do not have much information about it.
Here is my php code that I am trying to do
<?php
$first= new Carbon;
//$second= here I want the date to be collected from date column in the database
//X= The name of the item that will be collected from database (Item_Name) column
if ($second=eq($first+1))
{echo"X is going to expire tomorrow"}
// instade of dispaying the above sentence, i want it to be sent as email
if ($first=eq($second))
{echo"X is going to expire today!!"}
if ($first>eq($second))
{echo"X has EXPIRED already"}
?>
My table:
ID Item_Name Expiry_Date
1 Milk 2017-10-20
2 Chicken 2017-10-22
3 Meat 2017-10-25
You could use the DATE_ADD function to determine the interval.
SELECT
col1,
col2,
...
FROM
table
WHERE
`date_column` BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 1 DAY);
You mentioned PHP, and I want to clarify that this is MySQL, and I'm assuming you have a cron job or the like setup to run a PHP script.
UPDATE:
Based on your updated question, you have two major choices, either do the processing in the database or in PHP. The decision is dependent on how you planning on using it, but can feasibly be done both ways.
-- MySQL --
SELECT
ID,
Item_Name,
Expiry_Date,
CASE
WHEN `Expiry_Date` < CURDATE() THEN CONCAT(`Item_Name`,' has EXPIRED already.')
WHEN `Expiry_Date` = CURDATE() THEN CONCAT(`Item_Name`,' will expire today.')
WHEN `Expiry_Date` BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 1 DAY) THEN CONCAT(`Item_Name`,' will expire tomorrow.')
WHEN `Expiry_Date` > DATE_ADD(CURDATE(), INTERVAL 1 DAY) THEN CONCAT(`Item_Name`,' will expire after tomorrow.')
ELSE 'Error processing expiration date.'
END AS `Expiration_Message`
FROM
table;
Which would produce
ID Item_Name Expiry_Date Expiration_Message
1 Milk 2017-10-20 Milk will expire after tomorrow.
2 Chicken 2017-10-22 Chicken will expire after tomorrow.
3 Meat 2017-10-25 Meat will expire after tomorrow.
You could also do the string parsing and date addition in PHP which you seemed to start on. In terms of sending an email, PHP has a mail function, which How to send an email using PHP? might give you some guidance on. Now, I assume you want this to run on a schedule, so I would look into Execute PHP script in cron job.
Hope this helps with some more clarification.

Daily cron to poll mysql and update status based on timestamp

I have a db full of email users that lists the date and time they signed up in a column called signup_date using the DATETIME type (it uses now() ) and I also have an expire_date column which lists the same format but exactly a year later using ADDDATE(NOW(), INTERVAL 365 DAY)
I have added a status column with values being either 0 or 1. I guess this can be an ENUM type. Upon registration, status is set to 1 for active.
What I want is that if the timestamp of the expire_date is older than the current time, the cron should execute an update on that row of the user setting the status to 0. In postfix, I altered the query and appended the status=1 so that it will select the user with only status of 1. If the status isn't 1, then the user will not be found and won't be able to log in. This cron can run daily. I'm not too anal about having it run every second. Users can renew their emails within the next day. So this is my simple way of expiring emails, if they are not active or so after a year. What I need help with is constructing the cron. Should this be done with just php or does some bash need to be used? Im unclear of how to structure the script. Thanks.
Why maintain an additional flag column? You can calculate the status on the fly
SELECT *
FROM table_name
WHERE expire_date > NOW()
This will return only unexpired rows
If you need status to be produced as a column you can do
SELECT *, (expire_date > NOW()) status
FROM table_name

matching logs with schedule on flexi-time

I'm working on a module where the system would be able to determine where the logs of a flexi-time schedule belong...
Here's what I'm trying to do. I have a table called office_schedule with fields and values:
emp_ID time_in time_out
1 8:00:00 9:00:00
1 9:30:00 12:00:00
1 13:30:00 17:00:00
The example table Above 'office_schedule' Contains the values of schedule of a single employee in a single day. Given that I have another table called 'office_logs' with a value:
emp_ID log_in log_out
1 8:40:00 11:30:00
I searching for a query that would take the employee's logs and try to determine which value in 'office_schedule' table the logs belong to, by calculating the most value of time it has covered.
for example, if I query using the logs in 'office logs' table, it would match the second value of 'office_schedule' table, because the logs cover more span of time in the 'office_schedule' table's second value than the others.
i hope this is understandable enough.
please help...
Assuming the time cells are defined as TIME and not as VARCHAR, I would try something like that (but maybe there is a better way):
SELECT * FROM `office_logs` as log LEFT JOIN `office_schedule` AS sched ON log.`emp_ID` = sched.`emp_ID` WHERE log.`emp_ID` = 1 ORDER BY (ABS(sched.`Time_in` - log.`log_in`) + ABS(sched.`Time_out` - log.`log_out`)) ASC LIMIT 1;
It calculates the absolute difference between the log in and log out times of an employee to each of his scheduled time in and time out. The return is ordered by the smallest difference.
Maybe this helps.

check if it happens in 24 hours

Imagine we're giving users the ability to send emails using our website, but we want to limit them to not send more than 30 emails per day(24 hours).
So, by sending each email we're gonna insert a record into our table, then while he/she wants to send another one, we check if he has sent more than 30 emails during 24 hours or not.
How we could check this with PHP?
we query db, we got 20 records for this user, the date of records are:
2012-08-14 13:10:58
2012-08-14 12:45:47
2012-08-14 16:32:18
2012-08-14 19:10:40
...
...
...
How we could achieve such rule?
Thanks
Don't check it in PHP, use a simple query like this to get the answer out of he database right off the bat.
select
count(*)
from
yourTableName
where
dateCol>date_sub(now(), interval 1 day)
and userID=...
This will give you the count.
Edit: As Boris points out, this will count per day, you could change it to check for the last 24 hour period like this:
select
count(*)
from
yourTableName
where
dateCol>date_sub(now(), interval 24 hour)
and userID=...
Edit 2: After checking this, Riad correcly points out, these two do in fact return the same value. The 1 day is treated as exactly 1 day, not a calendar date. If the date column has a datetime of '2012-08-13: 13:00:00' a date_sub( dateCol, interval 1 day) will return '2012-08-12: 13:00:00'.

Send reminder: 2 months, 1 month and one week before expiration date

I was wondering how I can send reminder to my users (via email) two months, a month and one week before their account expiration date (eg. $accountExp which is a unix timestamp)?
set up a cron job to run a php script that will fetch entries in this way e.g.
/* send those 2 months before exp date*/
$sql='select * FROM users WHERE exp_date_unix>='.(time()+86400*60);
or if you use the datetime/timestamp field
the condition would be smth like
DATE_DIFF(exp_date_unix, CURRENT_TIMESTAMP) >=60;
You'll need to set up a cron job to work out if an email needs to be sent and send it, and use the date_diff sql function to calculate what needs to be sent in the script

Categories