l3rodey Posted January 10, 2014 Share Posted January 10, 2014 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! Quote Link to comment Share on other sites More sharing options...
davidannis Posted January 10, 2014 Share Posted January 10, 2014 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 } Quote Link to comment Share on other sites More sharing options...
l3rodey Posted January 10, 2014 Author Share Posted January 10, 2014 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... Quote Link to comment Share on other sites More sharing options...
l3rodey Posted January 10, 2014 Author Share Posted January 10, 2014 Warning: date_sub() expects parameter 1 to be DateTime, string given in /filename/ on line 5 date_sub($date, date_interval_create_from_date_string('14 days')); That is line 5 Quote Link to comment Share on other sites More sharing options...
davidannis Posted January 10, 2014 Share Posted January 10, 2014 I'll give it a try, fix, and repost. Quote Link to comment Share on other sites More sharing options...
davidannis Posted January 10, 2014 Share Posted January 10, 2014 Sorry, mangled the code for subtracting 14 days. I tried this and it seems to work: $dateTwoWeeksAgo = date('Y-m-d',strtotime('-2 weeks')); echo ' Give us a date of: '.$dateTwoWeeksAgo.'<br>'; Quote Link to comment Share on other sites More sharing options...
davidannis Posted January 10, 2014 Share Posted January 10, 2014 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). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.