0

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;
?>

4
Contributors
22
Replies
81
Views
2 Years
Discussion Span
Last Post by ronjacob012
Featured Replies
  • 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](http://php.net/manual/en/timezones.asia.php) for the correct timezone. Read More

0

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).

0
$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';
    }

}

Edited by ronjacob012

0

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.

Edited by ronjacob012

0

//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";
?>

Edited by ronjacob012

0

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.

Edited by ehpratah: typo

0

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

0

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

0

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.

0

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
}

Edited by ehpratah: typo

0

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

0

this error

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

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

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

?>

Edited by ehpratah: typo

0

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

0

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
}

?>
0

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();

Edited by ehpratah: typo

0

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

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.