简体   繁体   English

如何将php数据库中的日期与今天的日期进行比较

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

I have created a mysql table as shown below.我创建了一个 mysql 表,如下所示。

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我想使用 if else 语句将表中的日期与当前日期进行比较,并在前一天、后一天和同一日期向用户发送通知

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.例如,假设今天的日期是 18/10,牛奶的到期日期是 20/10,所以我想在 19 日、20 日和 21 日发送通知。

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 :).所以,如果你能帮助我比较代码和发送通知(例如电子邮件)的代码应该在 if 语句下,我将非常感激:)。

My question might seem easy but I am new to php I do not have much information about it.我的问题可能看起来很简单,但我是 php 新手,我没有太多关于它的信息。

Here is my php code that I am trying to do这是我正在尝试执行的 php 代码

<?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.您可以使用DATE_ADD函数来确定间隔。

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.您提到了 PHP,我想澄清一下,这是 MySQL,我假设您有一个 cron 作业或类似的设置来运行 PHP 脚本。

UPDATE: Based on your updated question, you have two major choices, either do the processing in the database or in PHP.更新:根据您更新的问题,您有两个主要选择,要么在数据库中进行处理,要么在 PHP 中进行处理。 The decision is dependent on how you planning on using it, but can feasibly be done both ways.该决定取决于您计划如何使用它,但可以通过两种方式进行。

-- MySQL -- -- 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.这将产生 ID Item_Name Expiry_Date Expiration_Message 1 Milk 2017-10-20 Milk 将在明天之后过期。 2 Chicken 2017-10-22 Chicken will expire after tomorrow. 2 鸡肉 2017-10-22 鸡肉将在明天之后过期。 3 Meat 2017-10-25 Meat will expire after tomorrow. 3 肉 2017-10-25 肉将在明天之后过期。

You could also do the string parsing and date addition in PHP which you seemed to start on.您还可以在 PHP 中进行字符串解析和日期添加,这似乎是您开始使用的。 In terms of sending an email, PHP has a mail function, which How to send an email using PHP?在发送邮件方面,PHP有mail功能, 如何使用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 .现在,我假设您希望它按计划运行,因此我将研究在 cron job 中执行 PHP 脚本

Hope this helps with some more clarification.希望这有助于更多的澄清。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM