0

PHP. I'm trying to compare two dates. One is dateofbirth stored in a MySQL table and the other is today's date without the year. if the date (m-d) stored in database matches today's date (m-d) a window will pop up.

This is for a Linux server, running MySQL 5, PHP 5.

$sql1 = "SELECT * FROM perfil_aluno WHERE id_usuarios = 
'".$_SESSION['usuarioId']."' ";
$query1 = mysqli_query($mysqli, $sql1);
$dados1 = mysqli_fetch_assoc($query1);

$dob = date('m-d', strtotime($dados1['dateofbirth']));
$2day = date('m-d');

if($dob == $2day) {
// execute some code...
}

If the month and day of birth (m-d) stored in the database matches today's date month and day, a window will pop up wishing the user a Happy Birthday.

1 Answers1

2

You could use the DateTime objects to compare the dates like so:

$date1 = DateTime::createFromFormat('!m-d', $dob);
$date2 = DateTime::createFromFormat('!m-d', $2day);

if ($date1 == $date2) {
    // execute some code...
}

The ! prevents taking the current timestamp instead of 00:00:00 so it would be safer to use it in a comparision without the time part needed. See in the comments here

Nick
  • 138,499
  • 22
  • 57
  • 95
Siad Ardroumli
  • 596
  • 2
  • 9