PHP如何将数据库中的日期与当前日期进行比较

时间:2012-07-09 09:36:57

标签: php mysql

我分别存储了第(10),第(4)和第(1993)年。如何将其与当前日期进行比较?如果日期匹配,则使用cron发送电子邮件至myemail@gmail.com。

这是代码:

<?php
include"connect_mysql.php";
$reminder_dates = mysql_query("SELECT*FROM registration_form");
while($row = mysql_fetch_array($reminder_dates)){
$main_dob_day = $row['main_dob_day'];
$main_dob_month = $row['main_dob_month'];   
$main_dob_year = $row['main_dob_year'];
}
?> 

3 个答案:

答案 0 :(得分:1)

$main_date   = "{$main_dob_year}-{$main_dob_month}-{$main_dob_day}";
$todays_date = date("Y-m-d");
$today       = strtotime($todays_date);
$main_dt     = strtotime($main_date);

if($main_dt == $today)
{
  $valid = "send email";
  echo $valid;
}

它会起作用

答案 1 :(得分:0)

也许是这样的:

$old_date = "2006-01-16"; // Create this from the 3 strings you have in the db
$old_date = strtotime($old_date); 

$todays_date = date("Y-m-d"); 
$todays_date = strtotime($todays_date); 


if ($old_date == $todays_date)
 print "equal";
else
 ....

编辑:在回复您的评论时,您可以使用日期功能获取当前日期和月份,如下所示:

$m = date("m");
$d = date("d");

答案 2 :(得分:0)

您可以在SQL查询中进行正确的比较,然后循环会将电子邮件发送到查询返回的所有行:

<?php
    include"connect_mysql.php";
    $sql = "
        SELECT *
        FROM registration_form
        WHERE 
            main_dob_month = MONTH(CURDATE()) AND 
            main_dob_day = DAYOFMONTH(CURDATE())";

    $reminder_dates = mysql_query($sql);
    while($row = mysql_fetch_array($reminder_dates))
    {
        // send email(s)
    }
?>