php mail() statements
-
05-07-2019 - |
Question
I am trying to get all the users information from the database and check the timestamp (fourteendays) and check it with the time now I got the time bit working but I can't get it to send to all the when the time is > fourteendays
$result1 = mysql_query("SELECT * FROM accounts") or die (mysql_error());
while ($row1 = mysql_fetch_array($result1)) {
$users_email = $row1['email'];
$user_name = $row1['user'];
$days_time = $row1['fourteendays'];
if($timenow > $days_time){
mail( $users_email, $subject, $emailbody, $headers);
echo "email sent!";
}
}
Solution
I recommend doing the date comparison in SQL, instead of pulling back all of the data in your query:
SELECT * FROM accounts WHERE DateDiff(Now(), fourteendays) > 14
Also, what is in that column in the database? If it is a timestamp, why don't you just call it as such? Am I missing something?
OTHER TIPS
Try. Thats greater than or equal to 14 days.
if($timenow >= $days_time){
mail( $users_email, $subject, $emailbody, $headers);
echo "email sent!";
}
Also SELECT * is bad practice. You should only select the fields you need in that query.
I assume you have already set the '$timenow' variable somewhere previous in the script and I would make sure to check that both time formats are the same when comparing. That was my problem in the past.
For date-related php information, visit the following link
Or even
SELECT * FROM accounts WHERE fourneendays < (NOW() - INTERVAL 14 DAYS);
I am assuming that NOW() gives the timestamp you want - it won't be in UTC (But you knew that, right)?