I wrote this php birthday script to send emails to people whos birthday is today. I know there is a more efficient way to do this, but because i did not use the standard unix timestamp in my mysql database. I had to do it this way which was a pain. Anybody have any suggestions how I could improve this.
One thing I wanted was to be able to put the persons name in the message of the email. But then I ran into the problem what if there are two people who have the same birthday. Then both there names are put into the message of the email.
Here is the code.
//calculate years of age (input string: YYYY-MM-DD)
function birthday ($birthday)
{
list($year,$month,$day) = explode("-",$birthday);
$year_diff = date("Y") - $year;
$month_diff = date("m") - $month;
$day_diff = date("d") - $day;
if ($day_diff < 0 or $month_diff < 0)
{
$year_diff--;
}
return "$month" . "$day";
}
//query for bdates in the database
$query3 = "SELECT * FROM users ORDER BY bdate";
$dbConnection = new DatabaseConnector();
$result = $dbConnection->runQuery($query3);
$dbConnection->disconnectFromDB();
$row= mysql_fetch_array($result);
//Going through all the bdates
while($row = mysql_fetch_array($result))
{
//Run the function to grab the month and day only
$bdates = birthday($row['bdate']);
//See what today is
$now = date("Ymd");
//Only grab the month and day from today
$now = substr($now, 4, 7);
//Seeing if there is a match then email them a birthday message
if($now == $bdates)
{
$email = $row['email'] . ",";
//Preparing for email
$subject = "Happy Birthday from Faceplace";
$message = "HAVE A GREAT BIRTHDAY!";
$header = "From:" . "Faceplace@sample.com" . " \r\n";
echo $fName;
mail($email, $subject, $message, $header);
}