0

I want to subtract two dates which will give me the difference of 15 days only and then send an automatic email without user interaction? How is that possible?

Here is my PHP Code

<?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);
?>
James Z
  • 12,209
  • 10
  • 24
  • 44
  • 1
    Possible duplicate of [PHP find difference between two datetimes](http://stackoverflow.com/questions/15688775/php-find-difference-between-two-datetimes) – Ken Apr 16 '16 at 18:00

2 Answers2

1

Simply try this one to find the different between two dates then do as you need

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
Ananda G
  • 2,389
  • 23
  • 39
0

I recommend using PHP's mail function for starters.

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

I do recommend if you are using script in a production type of environment is to use a dependency to do the hard work when you require more then what mail is capable of. (If your new to PHP I would start with PHP's mail)

https://github.com/PHPMailer/PHPMailer

To send the mail without user interaction is simply drop the mail function where you would want the email to send in your PHP script. I would need more specifics, but if you're going to use it in that script you posted just drop it in one of your conditions on when you want it to send.

ajm113
  • 936
  • 1
  • 8
  • 18