Jump to content

Send Email after set timestamp time


l3rodey

Recommended Posts

I need to be able to send an email with PHP after 2 weeks of my timestamp for example my database looks like this:

 

clientID    |    timestamp    |    sent

01           |    2014-01-10  |    0

02           |    2014-01-08  |    1

 

so sent 0 means it has not been sent, 1 means it has, so I will run a php code through a cron job and I need it to be able to check the time stamp if it's 2 weeks after the timestamp to do the following: which I will write

 

I just don't know how to check it the time stamp is over 2 weeks or a 1 week even or anything I have never tried to do this kind of coding before.

 

If someone could help it would be really cool! 

Link to comment
Share on other sites

Try something like this:

//open db here
$date = date('Y-m-d');
date_sub($date, date_interval_create_from_date_string('14 days'));
$query="SELECT * FROM mytable WHERE sent=0 and date<='$date'";
$result = mysqli_query($link, $query);
while ($row=mysqli_fetch_assoc($result)){
//send email
}
Link to comment
Share on other sites

Hi Thanks what about this?

 

<?php
//open db here
$date = date('Y-m-d');
date_sub($date, date_interval_create_from_date_string('14 days'));

$query="SELECT * FROM mytable WHERE sent=0 and completed<='$date'";
$result = mysqli_query($link, $query);
while ($row=mysqli_fetch_assoc($result)){
//send email
}
?>
completed is the table row which is a timestamp in the database, Is this correct? It's clearly hard to test without waiting it out... 
Link to comment
Share on other sites

Alternatively, if you want to stick with date_sub in my original code, this appears to work:

$date = date('Y-m-d');
$date1 = date_create($date);
date_sub($date1, date_interval_create_from_date_string('14 days'));
$dateTwoWeeksAgo = date_format($date1, 'Y-m-d');
echo ' method 2:  '.$dateTwoWeeksAgo.'<br>';

There is also a way to do the date calculation directly in the MySQL Query. I'm guessing somebody else could supply that SQL statement easier/better than I can (Barand is a master).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.