-1

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
Monzer
  • 33
  • 5

1 Answers1

1

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.

kchason
  • 2,836
  • 19
  • 25
  • Thnaks for ur comment. If i am not mistaken, I think DATE_ADD function is for adding dates, but the one i am trying to do is for comparing two dates and take action if the condition is true, I have included the code that I am working on it in the question, and I think it might help u to get a clear idea for what I am talking about. – Monzer Oct 18 '17 at 03:50
  • Sorry but I did not get your point, where exactly should I place that code, is it in sql query or ? as I am using phpmyadmin as local host. I have not created database before or learned about php but my project happened to have database and I am trying to get some information from here and there. – Monzer Oct 20 '17 at 04:37