如何在PHP中减去两个日期,并在没有交互的情况下发送自动电子邮件


How to subtract two dates in PHP and send an auto email without interaction

我想减去两个日期,只差15天,然后在没有用户交互的情况下发送自动电子邮件?这怎么可能?

这是我的PHP代码

<?php
$user = 'root';
$pass = '';
$con = 'pharmacy';
$con = new mysqli('127.0.0.1', $user, $pass, $con);
if(!$con)
{
echo 'Unable to Connect with Server';
}
if (!mysqli_select_db($con,'pharmacy'))
{
echo 'Database is Not Selected';
}
//select database
mysqli_select_db($con,'pharmacy');
//Expiry Date which is stored in Datebase 
//Convert String into Date Format
$sql_unique=$con->query("SELECT exp_dt FROM scandrug");
$expiry = strtotime($sql_unique);
$newformat = date('d-m-Y',$expiry);
$day= date('d',$newformat);   //Day of the month
$month= date('m',$newformat); //Month of the year
$year= date('Y',$newformat);  //Year
//echo $newformat;
//Obtain current Date
$current_dt = new DateTime();
$current_dt= date('d-m-Y');
$dayy= date('d',$current_dt);  //Day of the month
$monthh= date('m',$current_dt); //Month of the year
$yearr= date('Y',$current_dt);  //Year
//echo $current_dt;
do{
if($day-$dayy==15 || $day-$dayy==-15 && $month-$monthh==01 || $month-$monthh==00 && $year-$yearr==0000)
{
}
else
{
//nothing will happen
}
}while(1);
?>

只需尝试此项即可找到两个日期之间的差异,然后根据需要进行

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');

我建议初学者使用PHP的邮件函数。

http://php.net/manual/en/function.mail.php

如果您在生产类型的环境中使用脚本,我建议您在需要比邮件更多的内容时使用依赖项来完成艰苦的工作。(如果你是PHP新手,我会从PHP的邮件开始)

https://github.com/PHPMailer/PHPMailer

要在没有用户交互的情况下发送邮件,只需将邮件函数放在您希望在PHP脚本中发送电子邮件的位置即可。我需要更多的细节,但如果你要在你发布的脚本中使用它,只需在你希望它发送的时候将其放入你的条件之一。