Click here to Skip to main content
15,917,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Greetings everyone!

I'm trying to configure my system to save birthday wishes to my database, but I'm stuck with the PHP code. I need to check if today's month and day match the month and day in the birthday (Y-m-d) field dob of the database. I really appreciate your time and help. Thanks a million!

JC.

What I have tried:

PHP
if (date('Y-m-d') == $dados4['dob']) {
    $sql06 = "INSERT INTO comunicacao (enviar_para, titulo, descricao, tipo, curso, modulo, data_cadastro, hora_cadastro) VALUES ('".$_SESSION['usuarioId']."','Happy Birthday!','We wish you all the best on this very soecial day.','Todos','Todos','Todos','".date('Y-m-d')."','".date('H:i:s')."')";
    $query06 = mysqli_query($mysqli, $sql06);
}
Posted
Updated 23-May-19 0:44am
v2
Comments
[no name] 22-May-19 21:34pm    
You spelled "special" wrong.
Richard MacCutchan 23-May-19 8:16am    
Why are you storing that message in the database? The only item you need to save is the date of birth.
Richard Deeming 24-May-19 14:40pm    
PHP: SQL Injection - Manual[^]

In this particular instance, you might get away with it, assuming the $_SESSION['usuarioId'] is an integer, or at least something that can't be controlled by the user.

But using string concatenation / interpolation to build SQL queries a bad habit to get into, and will definitely leave your code vulnerable in the future.

1 solution

You only have to check that the day and month parts are equal for both values; the year part does not count.

PHP
$today = getdate();
$birthday = date_parse($dados4['dob']);
if (
      $today['mday'] == $birthday['mday']
   && $today['mon']  == $birthday['mon']
)
{
   /* Birthday */
}
else
{
   /* Not birthday */
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900