I want to create a webpage which will automatically send an email before 2 days.

<form action="try.php" method="post">

<p>Date From : <input type="date" name="date1" /></p>
<p>Date To: <input type="date" name="date2" /></p>
<input type="submit" name="submit" value="Submit" />
</form>

<?php

// echo("First name: " . $_POST['firstname'] . "<br />\n");

echo("Last name: " . $_POST['lastname'] . "<br />\n");

$date1Timestamp = strtotime($date1);
$date2Timestamp = strtotime($date2);
//Calculate the difference.
$difference = $date2Timestamp - $date1Timestamp;
echo $difference;
?>

Some thoughts . You say that you want to send automatically emails before 2 days. If you mean 2 days before today , then even the programming language that you will choose is not an issue , probably currently we can't move back in time (at least , in my knowledge). If you mean to send automatically emails 2 days before a given (future + 2 days) date , then there are many ways. Give us your code , or how do you think to do it and I believe that we will help. The code you gave is irrelevant with what you are asking (maybe the code is the real thing and the question is misleading , if so clarify it).

$sql = "SELECT * FROM bookings " .
    "WHERE DATE(date) > DATE(NOW()) " .
    "AND dateofquote != '' " .
    "AND email != '' " .
    "AND confirmed = 0";

$result = mysql_query($sql);
$num_rows = mysql_numrows($result);

$today = date('Y-m-d');
$count = 2;

if($num_rows){
while($row = mysql_fetch_assoc($result)){
    $date_of_quote = $row['dateofquote'];

    $datetime1 = new DateTime($today);
    $datetime2 = new DateTime($date_of_quote);
    $interval = $datetime1->diff($datetime2);
    $diff = $interval->format('%a');



    if($diff == '2'){
        // send email

    } else {
        echo 'something went wrong';
    }

}

sir can you give me one way how to send an email, for example the due date is june 10,2015 so the program must send an email 2 days before june 10, so in short it will send an email in june 8, 2015.

ok ... where is the send email code ?

//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$subject = 'Test email';
//define the message to be sent. Each line should be separated with \n
$message = "Hello World!\n\nThis is my first mail.";
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>

here is the email code sir

Hi

First you must create a script that will check your DB if if the duedate is 2 days before. a simple select query will work.something like

SELECT * FROM table WHERE DATEDIFF(NOW(), duedate) < 2

if the statement return true add your send mail code.

for it to send the email automatically(without any user interactions/page load) you need something like task scheduler or much better a cronjob which will run your script on a specific interval of time.

does the task scheduler capable of turning on the pc and the script will run then it will shutdown automatically everyday?

can you give me a sample using only the textbox wherein the user will input the dates? thanks in advance :)

No its not. the pc needs to be turned on for the task scheduler to work. the best option you have is yo use a cronjob on your webserver that will check your script on a given interval of time.

Your First script will work. you just need to make a cronjob for it to send the email automatically.

$currentdate=date_create("2014-06-15");
$duedate=date_create("2014-06-13");
$diff=date_diff($currentdate,$duedate);
echo $diff->format("%a");

if($diff=='2'){

    //send email
}else{
    //do nothing
}

sir it send me error
format("%a"); if($diff=='2'){ //send email }else{ //do nothing } ?>

what error?

this error

 format("%a"); if($diff=='2'){ //send email }else{ //do nothing } ?>

dont put your if statement in a straight line if you have a comment on it.

if($diff=='2'){ 
//send email 
}else
{ 
//do nothing 
} 

?>

ehpratah...please check the error and give your help about this error...

please be more specific what error are you getting? the sample i gave you is working perfectly on my side without any glitch.

Fatal error: Call to undefined function date_diff() in E:\Uploadedfiles\rjg\try\try3.php on line 4

<?php
$currentdate=date_create("2014-06-15");
$duedate=date_create("2014-06-13");
$diff=date_diff($currentdate,$duedate);
echo $diff->format("%a");


if($diff=='2'){
    echo "1";
}else{
    echo "2";//do nothing
}

?>

According to the php manual, date_diff is only available in PHP 5.3+ so thats why you are getting a fatal error. check the version of your php by echoing phpinfo();

sir im still getting the same error

<?php
date_default_timezone_set('Philippines');
$currentdate=date_create("2014-06-15");
$duedate=date_create("2014-06-13");
$diff=date_diff($currentdate,$duedate);
echo $diff->format("%a");
if($diff=='2'){
    echo "1";
}else{
    echo "2";//do nothing
}

?>

T_T

can you run this script?

<?php
echo phpinfo();
?>

please read http://php.net/manual/en/function.date-diff.php to better understand why you are getting that error message. Another thing your date_default_timezone is not correct please refer to this list for the correct timezone.

commented: will help you as soon as possible +0

ohhh exactly what I need sir ehpratah Thank you very much for assisting me.

let me know how to send mail before 2 days in codeigniter

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.